結果

問題 No.157 2つの空洞
コンテスト
ユーザー masa
提出日時 2015-02-27 00:13:46
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,566 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 843 ms
コンパイル使用メモリ 99,196 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-03-09 05:42:05
合計ジャッジ時間 1,582 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:40:26: warning: 'x' may be used uninitialized [-Wmaybe-uninitialized]
   40 |         que_start.push(x * 100 + y);
      |                        ~~^~~~~
main.cpp:27:13: note: 'x' was declared here
   27 |         int x, y;
      |             ^
main.cpp:40:32: warning: 'y' may be used uninitialized [-Wmaybe-uninitialized]
   40 |         que_start.push(x * 100 + y);
      |                        ~~~~~~~~^~~
main.cpp:27:16: note: 'y' was declared here
   27 |         int x, y;
      |                ^

ソースコード

diff #
raw source code

#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;
}
0