結果

問題 No.157 2つの空洞
ユーザー outlineoutline
提出日時 2021-01-19 03:19:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,744 bytes
コンパイル時間 1,505 ms
コンパイル使用メモリ 147,996 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-08 21:30:41
合計ジャッジ時間 2,298 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct UnionFind{
    int sz;
    vector<int> par;
    vector<int> rank;

    UnionFind(int n) : sz(n) {
        par.resize(sz);
        rank.assign(sz, 0);
        for(int i = 0; i < sz; ++i){
            par[i] = i;
        }
    }

    int find(int x){
        if(par[x] == x) return x;
        else    return par[x] = find(par[x]);
    }

    bool same(int x, int y){
        return find(x) == find(y);
    }

    bool unite(int x, int y){
        x = find(x), y = find(y);
        if(x == y)  return false;
        if(rank[x] < rank[y])   par[x] = y;
        else{
            par[y] = x;
            if(rank[x] == rank[y])  ++rank[x];
        }
        return true;
    }
};

constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};

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

    int W, H;
    cin >> W >> H;
    vector<vector<char>> C(H, vector<char>(W));
    for(int i = 0; i < H; ++i){
        for(int j = 0; j < W; ++j){
            cin >> C[i][j];
        }
    }
    
    UnionFind uf(H * W);
    for(int x = 0; x < H; ++x){
        for(int y = 0; y < W; ++y){
            if(C[x][y] == '#')  continue;
            for(int i = 0; i < 4; ++i){
                int nx = x + dx[i], ny = y + dy[i];
                if(nx < 0 || nx >= H || ny < 0 || ny >= W || C[nx][ny] == '#')  continue;
                uf.unite(x * W + y, nx * W + ny);
            }
        }
    }

    // for(int x = 0; x < H; ++x){
    //     for(int y = 0; y < W; ++y){
    //         cout << uf.find(x * W + y) << ' ';
    //     }cout << endl;
    // }

    vector<int> dist(H * W, INF);
    int s = -1;
    queue<int> que;
    for(int x = 0; x < H; ++x){
        for(int y = 0; y < W; ++y){
            if(C[x][y] == '#')  continue;
            int v = x * W + y;
            if(s == -1 || s == uf.find(v)){
                if(s == -1) s = uf.find(v);
                dist[v] = 0;
                que.emplace(v);
            }
        }
    }

    while(!que.empty()){
        int u = que.front();
        que.pop();
        int x = u / W, y = u % W;
        for(int i = 0; i < 4; ++i){
            int nx = x + dx[i], ny = y + dy[i];
            if(nx < 0 || nx >= H || ny < 0 || ny >= W)  continue;
            int v = nx * W + ny;
            if(C[nx][ny] == '#'){
                if(chmin(dist[v], dist[u] + 1)){
                    que.emplace(v);
                }
            }
            else if(s != uf.find(v)){
                if(chmin(dist[v], dist[u])){
                    que.emplace(v);
                }
            }
        }
    }

    int ans = INF;
    for(int x = 0; x < H; ++x){
        for(int y = 0; y < W; ++y){
            if(C[x][y] == '.' && s != uf.find(x * W + y)){
                chmin(ans, dist[x * W + y]);
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0