結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
古寺いろは
|
| 提出日時 | 2015-03-26 08:32:28 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,541 bytes |
| コンパイル時間 | 1,490 ms |
| コンパイル使用メモリ | 166,240 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-29 00:59:49 |
| 合計ジャッジ時間 | 2,186 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
bool check[30][30];
int dist[30][30];
int dy[] = {1, 0, -1, 0};
int dx[] = { 0, 1, 0, -1 };
int W, H;
bool ok(int y, int x){
return y >= 0 && x >= 0 && y < H && x < W;
}
int main() {
cin >> W >> H;
vector<string> board(H);
for (int i = 0; i < H; i++)
{
cin >> board[i];
}
queue<pair<int, int>> q;
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
if (board[i][j] == '.'){
check[i][j] = true;
q.push(make_pair(i, j));
break;
}
}
if (!q.empty()) break;
}
while (!q.empty()){
int y = q.front().first;
int x = q.front().second;
q.pop();
for (int k = 0; k < 4; k++)
{
int ny = y + dy[k];
int nx = x + dx[k];
if (!ok(ny, nx)) continue;
if (check[ny][nx]) continue;
if (board[ny][nx] != '.') continue;
check[ny][nx] = true;
q.push(make_pair(ny, nx));
}
}
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
if (board[i][j] == '.' && !check[i][j]){
check[i][j] = true;
q.push(make_pair(i, j));
dist[i][j] = 0;
}
else dist[i][j] = 999;
}
}
while (!q.empty()){
int y = q.front().first;
int x = q.front().second;
q.pop();
for (int k = 0; k < 4; k++)
{
int ny = y + dy[k];
int nx = x + dx[k];
if (!ok(ny, nx)) continue;
if (dist[ny][nx] < 999) continue;
if (board[ny][nx] == '.'){
cout << dist[y][x] << endl;
return 0;
}
check[ny][nx];
q.push(make_pair(ny, nx));
dist[ny][nx] = dist[y][x] + 1;
}
}
cout << "error" << endl;
}
古寺いろは