結果

問題 No.157 2つの空洞
ユーザー Captain
提出日時 2019-10-21 23:29:29
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,622 bytes
コンパイル時間 1,781 ms
コンパイル使用メモリ 179,808 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-02 17:53:00
合計ジャッジ時間 2,495 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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] = INF;
	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