結果

問題 No.157 2つの空洞
ユーザー latte0119latte0119
提出日時 2015-02-27 00:17:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 1,395 bytes
コンパイル時間 1,368 ms
コンパイル使用メモリ 53,216 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-06 03:01:33
合計ジャッジ時間 2,606 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
vector<pair<int,int> >s;
char fld[32][32];
int w,h;
const int INF=1e9;
struct data{
    int y,x,cost;
};
void dfs(int y,int x){
    if(y<0||y>=h||x<0||x>=w)return;
    if(fld[y][x]=='#')return;
    fld[y][x]='#';
    for(int i=0;i<4;i++)dfs(y+dy[i],x+dx[i]);
    s.push_back(make_pair(y,x));
}
int main(){
    scanf("%d%d",&w,&h);
    for(int i=0;i<h;i++)
        scanf("%s",fld[i]);

    for(int i=0;i<h;i++){
        bool flag=false;
        for(int j=0;j<w;j++){
            if(fld[i][j]=='.'){
                flag=true;
                dfs(i,j);
                break;
            }
        }
        if(flag)break;
    }

    int d[32][32],ans=INF;
    for(int i=0;i<s.size();i++){
        int sx=s[i].second,sy=s[i].first;
        fill_n(*d,32*32,INF);
        queue<data>Q;
        Q.push((data){sy,sx,0});
        while(Q.size()){
            data a=Q.front();Q.pop();
            if(a.y<0||a.y>=h||a.x<0||a.x>=w)continue;
            if(d[a.y][a.x]!=INF)continue;
            d[a.y][a.x]=a.cost;
            if(fld[a.y][a.x]=='.'){
                ans=min(ans,a.cost-1);
            }
            for(int i=0;i<4;i++){
                Q.push((data){a.y+dy[i],a.x+dx[i],a.cost+1});
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
0