結果

問題 No.157 2つの空洞
ユーザー けーむけーむ
提出日時 2020-07-09 11:20:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,082 bytes
コンパイル時間 2,268 ms
コンパイル使用メモリ 187,200 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-16 03:11:32
合計ジャッジ時間 3,093 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 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,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:106:31: warning: 'ng' may be used uninitialized [-Wmaybe-uninitialized]
  106 |                     q.push((S){ny, nx, 1, ng});
      |                               ^~~~~~~~~~~~~~~
main.cpp:100:49: note: 'ng' was declared here
  100 |                     ll rt = uf.root(y * w + x), ng;
      |                                                 ^~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

struct UnionFind {
    UnionFind() {}
    UnionFind(int n) {
        resize(n);
    }
    
    void resize(int n) {
        par.resize(n, -1);
    }
    
    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }
    
    bool merge(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return false;
        if (par[rx] > par[ry]) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
        return true;
    }
    
    bool is_same(int x, int y) {
        return (root(x) == root(y));
    }
    
    int size(int x) {
        return -par[root(x)];
    }
    
private:
    vector<int> par;
};

struct S {
    ll y, x, d, g;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    const ll inf = LONG_LONG_MAX / 2 - 1;
    const ll d[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
    ll w, h;
    cin >> w >> h;
    vector<string> c(h);
    rep(i, h) cin >> c[i];
    UnionFind uf(w * h);
    rep(y, h) {
        rep(x, w) {
            if (c[y][x] != '.') continue;
            ll idx = y * w + x;
            rep(i, 4) {
                ll ny = y + d[i][0];
                ll nx = x + d[i][1];
                ll ni = ny * w + nx;
                if ((ny < 0) || (ny >= h) || (nx < 0) || (nx >= w)) continue;
                if (c[ny][nx] != '.') continue;
                uf.merge(idx, ni);
            }
        }
    }
    set<ll> s;
    rep(y, h) {
        rep(x, w) {
            if (c[y][x] != '.') continue;
            s.insert(uf.root(y * w + x));
        }
    }
    vector<ll> grp;
    for(auto &x : s) grp.push_back(x);
    queue<S> q;
    vector<vector<vector<ll>>> dist(h, vector<vector<ll>>(w, vector<ll>(2, inf)));
    rep(y, h) {
        rep(x, w) {
            if (c[y][x] == '.') {
                rep(i, 4) {
                    ll ny = y + d[i][0];
                    ll nx = x + d[i][1];
                    if ((ny < 0) || (ny >= h) || (nx < 0) || (nx >= w)) continue;
                    if (c[ny][nx] != '#') continue;
                    ll rt = uf.root(y * w + x), ng;
                    rep(j, sz(grp)) {
                        if (grp[j] == rt) {
                            ng = j;
                        }
                    }
                    q.push((S){ny, nx, 1, ng});
                    dist[ny][nx][ng] = 1;
                }
            }
        }
    }
    while(!q.empty()) {
        S cur = q.front(); q.pop();
        if (dist[cur.y][cur.x][cur.g] != cur.d) continue;
        rep(i, 4) {
            ll ny = cur.y + d[i][0];
            ll nx = cur.x + d[i][1];
            ll nd = cur.d + 1;
            if ((ny < 0) || (ny >= h) || (nx < 0) || (nx >= w)) continue;
            if (c[ny][nx] == '.') {
                if (cur.g != uf.root(ny * w + nx)) {
                    dist[ny][nx][cur.g] = min(dist[ny][nx][cur.g], nd);
                }
            }
            else {
                if (dist[ny][nx][cur.g] > nd) {
                    dist[ny][nx][cur.g] = nd;
                    q.push((S){ny, nx, nd, cur.g});
                }
            }
        }
    }
    ll ans = inf;
    rep(y, h) {
        rep(x, w) {
            if (c[y][x] != '.') continue;
            ll g = uf.root(y * w + x);
            ll t = 1;
            rep(i, sz(grp)) {
                if (grp[i] == g) {
                    t -= i;
                    break;
                }
            }
            ans = min(ans, dist[y][x][t]);
        }
    }
    cout << (ans - 1) << endl;
    return 0;
}
0