結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-03-24 01:11:04 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 5,000 ms |
| コード長 | 1,394 bytes |
| コンパイル時間 | 745 ms |
| コンパイル使用メモリ | 65,844 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-13 10:50:40 |
| 合計ジャッジ時間 | 1,418 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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++) {
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;
}