結果

問題 No.157 2つの空洞
ユーザー evawenisevawenis
提出日時 2020-06-19 00:30:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,043 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 208,512 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-16 12:30:40
合計ジャッジ時間 3,096 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
vector<int>xw={1,0,-1,0},yw={0,-1,0,1};

void fill(int y,int x,vector<string>&c,vector<vector<int>>&mp,int f){
	if(f)mp.at(y).at(x)=1;
	else mp.at(y).at(x)=0;
	for(int i=0;i<4;++i){
		int nxy=y+yw.at(i),nxx=x+xw.at(i);
		if(nxy<0||nxy>=mp.size()||nxx<0||nxx>=mp.at(nxy).size()||c.at(nxy).at(nxx)!='.'||mp.at(nxy).at(nxx)!=-1)continue;
		fill(nxy,nxx,c,mp,f);
	}
}

int main(){
	cin.tie(0),ios::sync_with_stdio(false);
	int w,h; cin>>w>>h;
	vector<string>c(h); for(auto&&i:c)cin>>i;
	vector<vector<int>>mp(h,vector<int>(w,-1));
	for(int i=0,k=0;i<h;++i){
		for(int j=0;j<w;++j){
			if(c.at(i).at(j)!='.'||mp.at(i).at(j)!=-1)continue;
			fill(i,j,c,mp,k);
			++k;
		}
	}
	vector<pair<int,int>>one,oth;
	for(int i=0;i<h;++i){
		for(int j=0;j<w;++j){
			if(mp.at(i).at(j)==0)one.push_back({i,j});
			else if(mp.at(i).at(j)==1)oth.push_back({i,j});
		}
	}
	int ans=INT_MAX;
	for(auto&&i:one){
		for(auto&&j:oth){
			ans=min(ans,abs(i.first-j.first)+abs(i.second-j.second)-1);
		}
	}
	cout<<ans<<"\n"s;
}
0