結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-02-27 00:13:46 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,566 bytes |
| コンパイル時間 | 776 ms |
| コンパイル使用メモリ | 73,632 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-23 21:52:40 |
| 合計ジャッジ時間 | 1,712 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:42:16: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
42 | board[y][x] = 'S';
| ^
main.cpp:42:19: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
42 | board[y][x] = 'S';
| ^
ソースコード
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int w, h;
bool is_inside(int x, int y) {
return (0 <= x && x < w && 0 <= y && y < h);
}
int main() {
cin >> w >> h;
vector<string> board(h);
for (int i = 0; i < h; i++) {
cin >> board[i];
}
int x, y;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (board[i][j] == '.') {
x = j;
y = i;
break;
}
}
}
queue<int> que_start;
queue< pair<int, int> > que_wall;
que_start.push(x * 100 + y);
que_wall.push(make_pair(x * 100 + y, 0));
board[y][x] = 'S';
int nx, ny;
while (!que_start.empty()) {
int pos = que_start.front();
que_start.pop();
for (int i = 0; i < 4; i++) {
nx = pos / 100 + dx[i];
ny = pos % 100 + dy[i];
if (is_inside(nx, ny) && board[ny][nx] == '.') {
board[ny][nx] = 'S';
que_start.push(nx * 100 + ny);
que_wall.push(make_pair(nx * 100 + ny, 0));
}
}
}
int ans = -1;
while (!que_wall.empty() && ans == -1) {
pair<int, int> pos = que_wall.front();
que_wall.pop();
for (int i = 0; i < 4; i++) {
nx = pos.first / 100 + dx[i];
ny = pos.first % 100 + dy[i];
if (is_inside(nx, ny)) {
if (board[ny][nx] == '.') {
ans = pos.second;
break;
} else if (board[ny][nx] == '#') {
board[ny][nx] = '+';
que_wall.push(make_pair(nx * 100 + ny, pos.second + 1));
}
}
}
}
cout << ans << endl;
return 0;
}