結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-03-26 14:51:34 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,947 bytes |
| コンパイル時間 | 1,883 ms |
| コンパイル使用メモリ | 172,416 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2025-01-02 01:48:04 |
| 合計ジャッジ時間 | 2,847 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define FOR(i, a, b) for(int i = a; i < (int)b; ++i)
#define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i)
typedef long long ll;
typedef long double ld;
const int Inf = 1e9;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int w, h;
vector<string> s;
vector<vector<int> > g;
void bfs(int sx, int sy, int color) {
g[sx][sy] = color;
rep (i, 4) {
int nx, ny;
nx = sx + dx[i], ny = sy + dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
if (s[nx][ny] == '#') continue;
if (g[nx][ny] != 0) continue;
bfs(nx, ny, color);
}
}
int bfs(int sx, int sy) {
vector<vector<int> > dist(h, vector<int>(w, Inf));
queue<pair<int, int> > q;
q.push(make_pair(sx, sy));
dist[sx][sy] = 0;
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
rep (i, 4) {
int nx, ny;
nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
if (dist[nx][ny] != Inf) continue;
if (g[nx][ny] == 2) return dist[x][y];
dist[nx][ny] = dist[x][y] + 1;
q.push(make_pair(nx, ny));
}
}
return Inf;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(0);
cin >> w >> h;
s = vector<string>(h);
g = vector<vector<int> >(h, vector<int>(w, 0));
rep (i, h) cin >> s[i];
int color = 1;
rep (i, h) {
rep (j, w) {
if (s[i][j] == '.' && g[i][j] == 0) {
bfs(i, j, color);
color++;
}
}
}
int res = Inf;
rep (i, h) {
rep (j, w) {
if (g[i][j] == 1) res = min(res, bfs(i, j));
}
}
cout << res << endl;
return 0;
}