結果

問題 No.157 2つの空洞
ユーザー sakasu1sakasu1
提出日時 2020-08-11 00:11:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,531 bytes
コンパイル時間 1,742 ms
コンパイル使用メモリ 168,464 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 08:03:03
合計ジャッジ時間 2,291 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 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 1 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<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
    int dx[5]={1,-1,0,0},dy[5]={0,0,1,-1};
    int w,h,a[30][30],b[30][30],i,j;
    scanf("%d %d\n",&h,&w);
    for(i=0;i<=w+1;i++){
        for(j=0;j<=h+1;j++){
            a[i][j]=2,b[i][j]=(1<<29);
        }
    }
    queue<P> que;
    for(i=1;i<=w;i++){
        for(j=1;j<=h;j++){
            char s;
            scanf("%c",&s);
            if(s=='.'){
                a[i][j]=0;
                if(que.empty()){
                    que.push(P(i,j));
                    b[i][j]=0;
                }
            }
            else{
                a[i][j]=1;
            }
        }
        scanf("\n");
    }
    while(!que.empty()){
        P p=que.front();
        que.pop();
        for(i=0;i<4;i++){
            if(a[p.first+dx[i]][p.second+dy[i]]==0 && b[p.first+dx[i]][p.second+dy[i]]>b[p.first][p.second]){
                b[p.first+dx[i]][p.second+dy[i]]=b[p.first][p.second];
                que.push(P(p.first+dx[i],p.second+dy[i]));
            }
            if(a[p.first+dx[i]][p.second+dy[i]]==1 && b[p.first+dx[i]][p.second+dy[i]]>b[p.first][p.second]+1){
                b[p.first+dx[i]][p.second+dy[i]]=b[p.first][p.second]+1;
                que.push(P(p.first+dx[i],p.second+dy[i]));
            }
        }
    }
    for(i=1;i<=w;i++){
        for(j=1;j<=h;j++){
            if(a[i][j]==0 && b[i][j]!=0){
                cout << b[i][j] << endl;
                return 0;
            }
        }
    }
}
0