結果

問題 No.157 2つの空洞
ユーザー tokizotokizo
提出日時 2019-02-02 12:14:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,700 bytes
コンパイル時間 1,709 ms
コンパイル使用メモリ 174,668 KB
実行使用メモリ 9,008 KB
最終ジャッジ日時 2023-08-18 05:58:29
合計ジャッジ時間 5,214 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:136:34: 警告: ‘ppy’ may be used uninitialized [-Wmaybe-uninitialized]
  136 |                     if(D[ppx][ppy] + 1 < D[nx][ny]){
      |                        ~~~~~~~~~~^
main.cpp:123:22: 備考: ‘ppy’ はここで定義されています
  123 |             int ppx, ppy;
      |                      ^~~
メンバ関数 ‘int UnionFind::root(int)’ 内,
    inlined from ‘bool UnionFind::same(int, int)’ at main.cpp:37:20,
    inlined from ‘int main()’ at main.cpp:132:33:
main.cpp:21:17: 警告: ‘bp’ may be used uninitialized [-Wmaybe-uninitialized]
   21 |         if(par[x] < 0) return x;
      |                 ^
main.cpp: 関数 ‘int main()’ 内:
main.cpp:86:9: 備考: ‘bp’ はここで定義されています
   86 |     int bp;
      |         ^~

ソースコード

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;
    while(!q.empty()){
        pair<int, int> p = q.front();
        int px, py;
        px = p.first;
        py = p.second;
        D[px][py] = 0;

        queue<pair<int, int>> qq;
        for(int i = 0; i < 4; i++){
            int nx, ny;
            nx = px + dx[i];
            ny = py + dy[i];
            if(nx < 0 or ny < 0 or nx >= h or ny >= w) continue;
            if(C[nx][ny] == '#'){
                qq.push({nx, ny});
                D[nx][ny] = 1;
            }
        }
        while(!qq.empty()){
            pair<int, int> pp = qq.front();
            qq.pop();
            int ppx, ppy;
            ppx = pp.first;
            ppy == pp.second;
            for(int i = 0; i < 4; i++){
                int nx, ny;
                nx = px + dx[i];
                ny = py + dy[i];
                if(nx < 0 or ny < 0 or nx >= h or ny >= w) continue;
                if(C[nx][ny] == '.'){
                    if(!(uf.same(bp, nx * w + ny))){
                        ans = min(ans, D[ppx][ppy]);
                    }
                }else{
                    if(D[ppx][ppy] + 1 < D[nx][ny]){
                        D[nx][ny] = D[ppx][ppy] + 1;
                        qq.push({nx, ny});
                    }
                }
            }
        }
    }

    cout << ans << endl;

    /*
    for(int i = 0; i < h * w; i++){
        cout << uf.par[i] << ' ';
        if((i + 1) % w == 0) cout << endl;
    }
    */

    return 0;
}
0