結果

問題 No.157 2つの空洞
ユーザー masamasa
提出日時 2015-02-27 00:00:49
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,563 bytes
コンパイル時間 629 ms
コンパイル使用メモリ 74,932 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-06 02:57:20
合計ジャッジ時間 4,164 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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()) {
		auto 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) {
		auto 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