結果

問題 No.157 2つの空洞
コンテスト
ユーザー C
提出日時 2026-09-30 01:20:06
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 2,000 ms
+ 358µs
コード長 1,641 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,916 ms
コンパイル使用メモリ 399,840 KB
実行使用メモリ 9,920 KB
最終ジャッジ日時 2026-09-30 01:20:15
合計ジャッジ時間 7,302 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace atcoder;
using namespace __gnu_pbds;
using ll=long long;
using ld=long double;
using vll=vector<ll>;
using vvll=vector<vll>;
using pll=pair<ll,ll>;

// using mint=modint;

// template<class K,class V>
// using ordered_map=tree<K,V,less<K>,rb_tree_tag,tree_order_statistics_node_update>;

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

    ll W,H;
    cin>>W>>H;
    vector<string>g(H);
    for(int i=0;i<H;++i)cin>>g[i];

    ll sy=-1,sx=-1;
    for(int y=0;y<H;y++){
        for(int x=0;x<W;x++){
            if(g[y][x]=='.'){
                sy=y,sx=x;
                break;
            }
        }
        if(sy!=-1)break;
    }

    vvll dist(H,vll(W,LLONG_MAX));
    deque<pll>q;

    dist[sy][sx]=0;
    q.push_back({sy,sx});

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

    while(!q.empty()){
        auto [y,x]=q.front();
        q.pop_front();

        for(int dir=0;dir<4;++dir){
            ll ny=y+dy[dir],nx=x+dx[dir];
            if(ny<0||ny>=H||nx<0||nx>=W)continue;
            ll cost=(g[ny][nx]=='#');
            if(dist[ny][nx]>dist[y][x]+cost){
                dist[ny][nx]=dist[y][x]+cost;
                if(cost==0)q.push_front({ny,nx});
                else q.push_back({ny,nx});
            }
        }
    }

    ll ans=LLONG_MAX;

    for(int y=0;y<H;y++){
        for(int x=0;x<W;x++){
            if(g[y][x]=='.'&&dist[y][x]>0){
                ans=min(ans,dist[y][x]);
            }
        }
    }

    cout<<ans<<endl;

}
0