結果

問題 No.13 囲みたい!
ユーザー atn112323atn112323
提出日時 2016-05-04 21:39:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,040 bytes
コンパイル時間 685 ms
コンパイル使用メモリ 65,820 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:26:26
合計ジャッジ時間 1,290 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 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 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <utility>

using namespace std;

const int DX[] = {1, 0, -1, 0};
const int DY[] = {0, 1, 0, -1};

int W, H;
int M[100][100];
bool visited[100][100];

int main() {
	cin >> W >> H;
	for (int y = 0; y < H; y++) {
		for (int x = 0; x < W; x++) {
			cin >> M[x][y];
		}
	}
	for (int x = 0; x < W; x++) {
		for (int y = 0; y < H; y++) {
			if (!visited[x][y]) {
				queue<pair<int, int> > Q;
				Q.push(make_pair(x + y * W, -1));
				while (!Q.empty()) {
					pair<int, int> p = Q.front();
					Q.pop();
					int cx = p.first % W, cy = p.first / W;
					if (visited[cx][cy]) {
						cout << "possible" << endl;
						return 0;
					}
					visited[cx][cy] = true;
					for (int d = 0; d < 4; d++) {
						if (p.second == (d + 2) % 4) {
							continue;
						}
						int xx = cx + DX[d], yy = cy + DY[d];
						if (0 <= xx && xx < W && 0 <= yy && yy < H && M[xx][yy] == M[cx][cy]) {
							Q.push(make_pair(xx + yy * W, d));
						}
					}
				}
			}
		}
	}
	cout << "impossible" << endl;
	return 0;
}
0