結果

問題 No.157 2つの空洞
ユーザー fiordfiord
提出日時 2015-07-08 17:04:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,253 bytes
コンパイル時間 637 ms
コンパイル使用メモリ 68,604 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 09:36:10
合計ジャッジ時間 1,571 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
std::vector<std::string> c;
int w,h;
int x[]={1,-1,0,0},y[]={0,0,1,-1};
std::queue<std::pair<int,int> > next;

bool check(int nx,int ny){
	if(!(0<=nx&&nx<w))	return false;
	if(!(0<=ny&&ny<h))	return false;
	return true;
}

void groupdfs(int nh,int nw,char group){
	c[nh][nw]=group;
	if(group=='0')	next.push(std::make_pair(nh,nw));
	for(int i=0;i<4;i++){
		if(!check(nw+x[i],nh+y[i]))	continue;
		if(c[nh+y[i]][nw+x[i]]=='.')	groupdfs(nh+y[i],nw+x[i],group);
	}
	return;
}

int main(){
	std::cin>>w>>h;
	std::string in;
	for(int i=0;i<h;i++){
		std::cin>>in;
		c.push_back(in);
	}
	char group='0';
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(c[i][j]=='.'){
				groupdfs(i,j,group);
				group='g';
			}
		}
	}
	while(!next.empty()){
		int nx=next.front().second,ny=next.front().first;
		next.pop();
		for(int i=0;i<4;i++){
			if(!check(nx+x[i],ny+y[i]))	continue;
			if(c[ny+y[i]][nx+x[i]]=='#'){
				c[ny+y[i]][nx+x[i]]=c[ny][nx]+1;
				next.push(std::make_pair(ny+y[i],nx+x[i]));
			}
			else if(c[ny+y[i]][nx+x[i]]=='g'){
				int ans=0;
				for(char a='0';a<c[ny][nx];a++)	ans++;
				std::cout<<ans<<std::endl;
				return 0;
			}
		}
	}
	return 0;
}
0