結果

問題 No.13 囲みたい!
ユーザー fine
提出日時 2017-02-26 19:54:37
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 932 bytes
コンパイル時間 1,739 ms
コンパイル使用メモリ 166,800 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-11 15:40:42
合計ジャッジ時間 2,394 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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