結果

問題 No.157 2つの空洞
ユーザー Mcpu3Mcpu3
提出日時 2019-08-15 14:27:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 3,422 bytes
コンパイル時間 1,189 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 19:01:29
合計ジャッジ時間 2,072 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
	int W, H, a = 0, ans = INT_MAX;
	cin >> W >> H;
	vector<string> C(H);
	for (string& i : C) cin >> i;
	queue<pair<int, int>> b;
	vector<vector<int>> c(H, vector<int>(W));
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			if (!c[i][j] && '.' == C[i][j]) {
				++a;
				b.emplace(i, j);
				c[i][j] = a;
				while (!b.empty()) {
					if (!c[b.front().first - 1][b.front().second] && '.' == C[b.front().first - 1][b.front().second]) {
						b.emplace(b.front().first - 1, b.front().second);
						c[b.front().first - 1][b.front().second] = a;
					}
					if (!c[b.front().first][b.front().second - 1] && '.' == C[b.front().first][b.front().second - 1]) {
						b.emplace(b.front().first, b.front().second - 1);
						c[b.front().first][b.front().second - 1] = a;
					}
					if (!c[b.front().first][1 + b.front().second] && '.' == C[b.front().first][1 + b.front().second]) {
						b.emplace(b.front().first, 1 + b.front().second);
						c[b.front().first][1 + b.front().second] = a;
					}
					if (!c[1 + b.front().first][b.front().second] && '.' == C[1 + b.front().first][b.front().second]) {
						b.emplace(1 + b.front().first, b.front().second);
						c[1 + b.front().first][b.front().second] = a;
					}
					b.pop();
				}
			}
		}
	}
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			if (1 == c[i][j]) {
				for (int k = 0; k < H; ++k) {
					for (int l = 0; l < W; ++l) {
						if (2 == c[k][l]) {
							b.emplace(i, j);
							vector<vector<int>> d(H, vector<int>(W, -1));
							d[i][j] = 0;
							while (!b.empty()) {
								if (H - 1 != b.front().first) {
									if (-1 == d[1 + b.front().first][b.front().second]) {
										b.emplace(1 + b.front().first, b.front().second);
										d[1 + b.front().first][b.front().second] = d[b.front().first][b.front().second];
										if ('#' == C[b.front().first][b.front().second]) ++d[1 + b.front().first][b.front().second];
									}
								}
								if (W - 1 != b.front().second) {
									if (-1 == d[b.front().first][1 + b.front().second]) {
										b.emplace(b.front().first, 1 + b.front().second);
										d[b.front().first][1 + b.front().second] = d[b.front().first][b.front().second];
										if ('#' == C[b.front().first][b.front().second]) ++d[b.front().first][1 + b.front().second];
									}
								}
								if (b.front().first) {
									if (-1 == d[b.front().first - 1][b.front().second]) {
										b.emplace(b.front().first - 1, b.front().second);
										d[b.front().first - 1][b.front().second] = d[b.front().first][b.front().second];
										if ('#' == C[b.front().first][b.front().second]) ++d[b.front().first - 1][b.front().second];
									}
								}
								if (b.front().second) {
									if (-1 == d[b.front().first][b.front().second - 1]) {
										b.emplace(b.front().first, b.front().second - 1);
										d[b.front().first][b.front().second - 1] = d[b.front().first][b.front().second];
										if ('#' == C[b.front().first][b.front().second]) ++d[b.front().first][b.front().second - 1];
									}
								}
								b.pop();
							}
							ans = min(ans, d[k][l]);
						}
					}
				}
			}
		}
	}
	cout << ans;
}
0