結果

問題 No.157 2つの空洞
ユーザー CaptainCaptain
提出日時 2019-10-24 16:46:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,572 bytes
コンパイル時間 2,027 ms
コンパイル使用メモリ 176,884 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-23 04:29:47
合計ジャッジ時間 3,152 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

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;
	bool is = true;
	rep(i, H) {
		rep(j, W) {
			cin >> maze[i][j];
			if (is) {
				if (maze[i][j] == '.') {
					dist[i][j] = INF;
					que.push(make_pair(i, j));
					q.push(make_pair(i, j));
					is = false;
				}
			}
		}
	}

	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