結果

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

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;

struct Point {
	int x;
	int y;
};

int main() {
	const int MAX_HEIGHT = 100;
	const int MAX_WIDTH = 100;
	int dx[] = { -1, 1, 0, 0 };
	int dy[] = { 0, 0, -1, 1 };

	int map[MAX_WIDTH][MAX_HEIGHT];
	bool visited[MAX_WIDTH][MAX_HEIGHT];

	int w, h;
	cin >> w >> h;

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			visited[x][y] = false;
		}
	}

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> map[x][y];
		}
	}

	queue<pair<Point, Point>> nodes;
	for (int x = 0; x < w; x++) {
		for (int y = 0; y < h; y++) {
			if (visited[x][y])
				continue;

			Point start{ x, y };
			nodes.push(make_pair(start, start));
			visited[x][y] = true;
			while (!nodes.empty()) {
				Point current = nodes.front().first;
				Point prev = nodes.front().second;
				nodes.pop();
				for (int dir = 0; dir < 4; dir++) {
					int nx = current.x + dx[dir];
					int ny = current.y + dy[dir];

					if (nx < 0 || ny < 0 || nx >= w || ny >= h)
						continue;

					if (nx == prev.x && ny == prev.y)
						continue;

					if (map[nx][ny] != map[current.x][current.y])
						continue;

					if (visited[nx][ny]) {
						cout << "possible" << endl;
						return 0;
					}

					visited[nx][ny] = true;
					nodes.push(make_pair(Point{ nx, ny }, current));
				}
			}
		}
	}

	cout << "impossible" << endl;

	return 0;
}
0