結果

問題 No.157 2つの空洞
ユーザー masamasa
提出日時 2015-02-27 00:13:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,566 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 73,780 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 03:00:18
合計ジャッジ時間 1,530 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:42:9: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  board[y][x] = 'S';
         ^
main.cpp:42:12: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  board[y][x] = 'S';
            ^

ソースコード

diff #

#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