結果

問題 No.157 2つの空洞
ユーザー CaptainCaptain
提出日時 2019-10-24 16:40:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,515 bytes
コンパイル時間 2,044 ms
コンパイル使用メモリ 176,876 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-23 04:20:28
合計ジャッジ時間 2,804 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
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 = 1000000007;

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));
	vector<vector<int>>dist(H, vector<int>(W, -1));
	queue<pair<int, int>>que;
	queue<pair<int, int>>q;
	rep(i, H) {
		rep(j, W) {
			cin >> maze[i][j];
			if (maze[i][j] == '.') {
				dist[i][j] = INF;
				que.push(make_pair(i, j));
				q.push(make_pair(i, j));
			}
		}
	}

	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));
			q.push(make_pair(ny, nx));
		}
	}

	int ans = INF;
	while (!q.empty()) {
		pair<int, int>N = q.front();q.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;
			}
			q.push(make_pair(ny, nx));
		}
	}
	cout << ans << "\n";

	return 0;
}
0