結果

問題 No.157 2つの空洞
ユーザー take000take000
提出日時 2020-04-19 15:53:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,331 bytes
コンパイル時間 1,892 ms
コンパイル使用メモリ 175,720 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 15:06:23
合計ジャッジ時間 2,730 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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;
}
0