結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-03-24 00:49:33 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 1,435 bytes | 
| コンパイル時間 | 609 ms | 
| コンパイル使用メモリ | 65,832 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-13 10:50:22 | 
| 合計ジャッジ時間 | 1,262 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
#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++) {
			int num;
			cin >> num;
			map[x][y] = num;
		}
	}
	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 (nx == start.x && ny == start.y) {
						cout << "possible" << endl;
						return 0;
					}
					visited[nx][ny] = true;
					nodes.push(make_pair(Point{ nx, ny }, current));
				}
			}
		}
	}
	cout << "impossible" << endl;
	return 0;
}
            
            
            
        