結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-19 15:53:57 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,331 bytes |
| コンパイル時間 | 1,843 ms |
| コンパイル使用メモリ | 177,736 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-05 13:33:06 |
| 合計ジャッジ時間 | 2,731 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
using namespace std;
const int INF = 1e9;
int main() {
int H, W;
cin >> W >> H;
vector<string> S(H);
rep(i, H) cin >> S[i];
vector<vector<pair<int, int>>> hole(2);
auto bfs = [&](int cn, int y, int x) {
queue<pair<int, int>> que;
que.push({y, x});
S[y][x] = '#';
hole[cn].push_back({y, x});
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
while (!que.empty()) {
pair<int, int> now = que.front();
que.pop();
rep(i, 4) {
int ny = now.first + dy[i];
int nx = now.second + dx[i];
if (ny >= 0 && ny < H && nx >= 0 && nx < W && S[ny][nx] == '.') {
hole[cn].push_back({ny, nx});
S[ny][nx] = '#';
que.push({ny, nx});
}
}
}
};
int cnt = 0;
rep(i, H) rep(j, W) {
if (S[i][j] == '.') {
bfs(cnt, i, j);
++cnt;
}
}
int ans = INF;
for (auto u : hole[0]) {
for (auto v : hole[1]) {
ans = min(ans, abs(u.first - v.first) + abs(u.second - v.second) - 1);
}
}
cout << ans << endl;
return 0;
}