結果

問題 No.157 2つの空洞
ユーザー Mcpu3
提出日時 2019-08-15 14:27:13
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 3,422 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 87,116 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-19 15:17:21
合計ジャッジ時間 1,739 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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