結果

問題 No.157 2つの空洞
ユーザー CaptainCaptain
提出日時 2019-10-21 23:27:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,620 bytes
コンパイル時間 1,835 ms
コンパイル使用メモリ 178,772 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-15 15:08:20
合計ジャッジ時間 2,983 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll =long long;
#define rep(i,N) for(int (i)=0;(i)<(N);++(i))
#define SORT(i) sort((i).begin(),(i).end())
constexpr ll INF = 2000000000;
constexpr ll mod = 10e8 + 7;

int y[4] = { 0,1,0,-1 };
int x[4] = { 1,0,-1,0 };

int main() {
	int W, H; cin >> W >> H;
	vector<vector<char>>maze(H, vector<char>(W));
	pair<int, int>syx;
	bool is = true;
	rep(i, H) {
		rep(j, W) {
			cin >> maze[i][j];
			if (is) {
				if (maze[i][j] == '.') {
					syx = make_pair(i, j);
					is = false;
				}
			}
		}
	}
	vector<vector<int>>dist(H, vector<int>(W, -1));
	queue<pair<int, int>>que;
	dist[syx.first][syx.second] = 0;
	que.push(syx);
	int ny, nx;
	while (!que.empty()) {
		pair<int, int>N = que.front(); que.pop();
		rep(i, 4) {
			ny = N.first + y[i], nx = N.second + x[i];
			if (ny < 0 || nx < 0 || ny >= H || nx >= W || dist[ny][nx] != -1 || maze[ny][nx] == '#')continue;
			dist[ny][nx] = INF;
			que.push(make_pair(ny, nx));
		}
	}
	rep(i, H) {
		rep(j, W) {
			if (dist[i][j] == INF)que.push(make_pair(i, j));
		}
	}
	int ans = INF;
	while (!que.empty()) {
		pair<int, int>N = que.front();que.pop();
		rep(i, 4) {
			ny = N.first + y[i], nx = N.second + x[i];
			if (ny < 0 || nx < 0 || ny >= H || nx >= W || dist[ny][nx] != -1)continue;
			if (dist[N.first][N.second] == INF) {
				dist[ny][nx] = 1;
			}
			else if (maze[ny][nx] == '.') {
				ans = min(ans, dist[N.first][N.second]);
				dist[ny][nx] = dist[N.first][N.second] + 1;
			}
			else {
				dist[ny][nx] = dist[N.first][N.second] + 1;
			}
			que.push(make_pair(ny, nx));
		}
	}
	cout << ans << "\n";

	return 0;
}
0