結果

問題 No.13 囲みたい!
ユーザー finefine
提出日時 2017-02-26 19:54:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 932 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 165,332 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 08:48:49
合計ジャッジ時間 2,496 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,384 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int dh[] = {1, -1, 0, 0};
const int dw[] = {0, 0, 1, -1};

int W, H;
int g[100][100], used[100][100];

bool dfs(int ch, int cw, int ph, int pw) {
	used[ch][cw] = g[ch][cw];
	for (int i = 0; i < 4; i++) {
		int nh = ch + dh[i], nw = cw + dw[i];
		if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
		if (nh == ph && nw == pw) continue;
		if (g[ch][cw] != g[nh][nw]) continue;
		if (used[nh][nw] == g[nh][nw]) return true;
		if (dfs(nh, nw, ch, cw)) return true;
	}
	return false;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> W >> H;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> g[i][j];
		}
	}
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (g[i][j] == used[i][j]) continue;
			if (dfs(i, j, -1, -1)) {
				cout << "possible" << endl;
				return 0;
			}
		}
	}
	cout << "impossible" << endl;
	return 0;
}
0