結果

問題 No.157 2つの空洞
ユーザー tokizotokizo
提出日時 2019-02-02 12:31:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,739 bytes
コンパイル時間 2,510 ms
コンパイル使用メモリ 172,504 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 06:22:31
合計ジャッジ時間 3,464 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int h, w;
const int MAX = 30;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

char C[MAX][MAX];
bool seen[MAX][MAX];
int D[MAX][MAX];

struct UnionFind{
    vector<int> par;
    UnionFind(int N) : par(N){
        for(int i = 0; i < N; i++){
            par[i] = -1;
        }
    }
    int root(int x){
        if(par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    void unite(int x, int y){
        int rx = root(x);
        int ry = root(y);
        if(rx == ry) return;
        else{
            if(par[rx] == par[ry]){
                if(rx > ry) swap(rx, ry);
            }else if(par[rx] > par[ry]) swap(rx, ry);
            par[rx] += par[ry];
            par[ry] = rx;
       }
    }
    bool same(int x, int y){
        return root(x) == root(y);
    }
    void check(){
        for(auto x : par){
            cout << x << " ";
        }
        cout << endl;
    }
};

void dfs(int x, int y, UnionFind &uf){
    seen[x][y] = true;
    for(int i = 0; i < 4; i++){
        int nx, ny;
        nx = dx[i] + x;
        ny = dy[i] + y;
        if(nx < 0 or ny < 0 or nx >= h or ny >= w) continue;
        if(C[nx][ny] == '#' or seen[nx][ny]) continue;
        uf.unite(x * w + y, nx * w + ny);
        dfs(nx, ny, uf);
    }
    return;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> w >> h;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> C[i][j];
            D[i][j] = 1e9;
        }
    }

    UnionFind uf(h * w);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(C[i][j] == '.'){
                if(!seen[i][j]){
                    dfs(i, j, uf);
                }
            }
        }
    }

    queue<pair<int, int>> q;
    bool f = true;
    int bp;
    for(int i = 1; i < h - 1; i++){
        for(int j = 1; j < w - 1; j++){
            if(C[i][j] == '#') continue;
            int r = uf.root(i * w + j);
            if(f and uf.par[r] != -1){
                bp = r;
                f = false;
                q.push({i, j});
            }else if(!f and r == bp){
                q.push({i, j});
            }
        }
    }

    int ans = 1e9;
    for(int i = 1; i < h - 1; i++){
        for(int j = 1; j < w - 1; j++){
            if(C[i][j] == '#') continue;
            for(int k = 1; k < h - 1; k++){
                for(int l = 1; l < w - 1; l++){
                    if(C[k][l] == '#') continue;
                    if(!uf.same(i * w + j, k * w + l)){
                        ans = min(ans, abs(i - k) + abs(j - l) - 1);
                    }
                }
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0