結果

問題 No.157 2つの空洞
ユーザー MayimgMayimg
提出日時 2019-01-10 22:59:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,118 bytes
コンパイル時間 1,983 ms
コンパイル使用メモリ 176,996 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-16 06:09:34
合計ジャッジ時間 3,213 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
vector<string> g;
vector<vector<pair<int, int>>> a(2);
int h, w;
void wfs(int x, int y, int z) {
	queue<pair<int, int>> que;
	que.push(make_pair(x, y));
	while(que.size()) {
		int s = que.front().first;
		int t = que.front().second;
		que.pop();
		a[z].emplace_back(s, t);
		g[s][t] = '#';
		for(int i = 0; i < 4; i++) {
			int ss = s + dx[i];
			int tt = t + dy[i];
			if(ss < h && ss >= 0 && tt < w && tt >= 0 && g[ss][tt] == '.') {
				g[ss][tt] = '#';
				que.push(make_pair(ss, tt));
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);  
	cin >> w >> h;
	g.resize(h);
	for(int i = 0; i < h; i++) {
		cin >> g[i];
	}
	int flag = 0;
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			if(g[i][j] == '.') {
				wfs(i, j, flag);
				flag++;
			}
		}
	}
	int ans = 1 << 30;
	for(int i = 0; i < a[0].size(); i++) {
		for(int j = 0; j < a[1].size(); j++) {
			ans = min(ans, abs(a[0][i].first - a[1][j].first) + abs(a[0][i].second - a[1][j].second) - 1);
		}
	}
	cout << ans << endl;
	return 0;
}
0