結果

問題 No.13 囲みたい!
ユーザー assy1028assy1028
提出日時 2015-03-21 16:09:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,721 bytes
コンパイル時間 1,076 ms
コンパイル使用メモリ 84,624 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:17:32
合計ジャッジ時間 1,905 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:70:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |     scanf("%d%d", &w, &h);
      |     ~~~~~^~~~~~~~~~~~~~~~
main.cpp:76:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   76 |             scanf("%d", &m[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;

#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef pair<int, int> P;
typedef unsigned int uint;
typedef unsigned long long ull;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};

const int inf = 1000000009;
int m[102][102];
bool used[102][102];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

bool dfs(int x, int y, int prevx, int prevy) {
    used[x][y] = true;
    for (int i = 0; i < 4; i++) {
	int X = x + dx[i], Y = y + dy[i];
	if (!(X == prevx && Y == prevy) && m[X][Y] == m[x][y]) {
	    if (used[X][Y])
		return true;
	    if (dfs(X, Y, x, y))
		return true;
	}
    }
    return false;
}

bool solve(int h, int w) {
    for (int i = 1; i <= h; i++) {
	for (int j = 1; j <= w; j++) {
	    if (!used[i][j]) {
		if (dfs(i, j, -1, -1))
		    return true;
	    }
	}
    }
    return false;
}

int main() {
    int w, h;
    scanf("%d%d", &w, &h);

    memset(m, 0, sizeof(m));
    memset(used, false, sizeof(used));
    for (int i = 1; i <= h; i++) {
	for (int j = 1; j <= w; j++) {
	    scanf("%d", &m[i][j]);
	}
    }
    printf("%s\n", solve(h, w) ? "possible" : "impossible");
}
0