結果

問題 No.13 囲みたい!
ユーザー NotationNapNotationNap
提出日時 2015-04-23 05:30:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 852 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 160,392 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:18:16
合計ジャッジ時間 2,370 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 5 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

typedef long long Int;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);++(i))

int W, H;
int a[110][110];

vector<int> g[10000];
bool vis[10000];
bool cycle = false;
void dfs(int p, int prev) {
	if (vis[p]) {
		cycle = true;
		return;
	}
	vis[p] = true;
	REP(i, g[p].size()) if (prev != g[p][i]) {
		dfs(g[p][i], p);
	}
}

int main() {
	cin >> W >> H;
	REP(i, H) REP(j, W) cin >> a[i][j];

	int di[] = {0, 1, 0, -1};
	int dj[] = { 1, 0, -1, 0 };
	REP(i, H) REP(j, W) {
		REP(k, 4) {
			int ni = i + di[k];
			int nj = j + dj[k];
			if (0 <= ni && ni < H && 0 <= nj && nj < W && a[i][j] == a[ni][nj]) {
				int p1 = i * W + j;
				int p2 = ni * W + nj;
				g[p1].push_back(p2);
			}
		}
	}
	cycle = false;
	REP(p, H * W) if (!vis[p]) {
		dfs(p, -1);
	}
	cout << (cycle ? "possible " : "impossible ") << endl;
}
0