結果

問題 No.157 2つの空洞
ユーザー furafura
提出日時 2020-04-18 18:23:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,044 bytes
コンパイル時間 2,192 ms
コンパイル使用メモリ 202,360 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 21:06:38
合計ジャッジ時間 3,271 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

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

int main(){
	int h,w; scanf("%d%d",&w,&h);
	string B[20];
	rep(i,h) cin>>B[i];

	int i0=-1,j0=-1;
	rep(i,h) rep(j,w) if(i0==-1 && B[i][j]=='.') { i0=i; j0=j; }

	bool ini[20][20]={};
	ini[i0][j0]=true;
	queue<pair<int,int>> Q; Q.emplace(i0,j0);
	while(!Q.empty()){
		int y,x; tie(y,x)=Q.front(); Q.pop();
		rep(k,4){
			int yy=y+dy[k],xx=x+dx[k];
			if(0<=yy && yy<h && 0<=xx && xx<w && B[yy][xx]=='.' && !ini[yy][xx]){
				ini[yy][xx]=true;
				Q.emplace(yy,xx);
			}
		}
	}

	int d[20][20]={};
	rep(y,h) rep(x,w) {
		if(ini[y][x]){
			d[y][x]=0;
			Q.emplace(y,x);
		}
		else{
			d[y][x]=-1;
		}
	}

	while(!Q.empty()){
		int y,x; tie(y,x)=Q.front(); Q.pop();
		rep(k,4){
			int yy=y+dy[k],xx=x+dx[k];
			if(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx]==-1){
				d[yy][xx]=d[y][x]+1;
				if(B[yy][xx]=='.'){
					printf("%d\n",d[yy][xx]-1);
					return 0;
				}
				Q.emplace(yy,xx);
			}
		}
	}

	return 0;
}
0