結果

問題 No.157 2つの空洞
ユーザー woodsgreen
提出日時 2022-12-17 16:37:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,076 bytes
コンパイル時間 1,639 ms
コンパイル使用メモリ 198,484 KB
最終ジャッジ日時 2025-02-09 15:19:38
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
struct P{
    int x,y;
} q[410],s[410],t[410];
int w,h,cnts,cntt,vis[30][30];
char a[30][30];
void bfs(int x,int y){
    memcpy(t,s,sizeof(s));
    cntt=cnts, cnts=0;
    queue<P> q;
    q.push((P){x,y});
    vis[x][y]=1;
    while(!q.empty()){
        P p=q.front(); q.pop();
        s[cnts++]=(P){p.x,p.y};
        for(int i=0;i<4;++i){
            int xx=p.x+dx[i],yy=p.y+dy[i];
            if(!vis[xx][yy]&&a[xx][yy]=='.'){
                vis[xx][yy]=1;
                q.push((P){xx,yy});
            }
        }
    }
}
int main(){
    cin>>h>>w;
    for(int i=1;i<=w;++i)
        for(int j=1;j<=h;++j)
            cin>>a[i][j];
    for(int i=1;i<=w;++i)
        for(int j=1;j<=h;++j)
            if(!vis[i][j]&&a[i][j]=='.')
                bfs(i,j);
    int minn=100;
    for(int i=0;i<cnts;++i)
        for(int j=0;j<cntt;++j){
            int d=abs(s[i].x-t[j].x)+abs(s[i].y-t[j].y);
            if(minn>d)
                minn=d;
        }
    cout<<minn-1;
    return 0;
}
0