結果

問題 No.157 2つの空洞
ユーザー dnishdnish
提出日時 2017-05-04 12:31:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,041 bytes
コンパイル時間 1,719 ms
コンパイル使用メモリ 174,268 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 07:54:39
合計ジャッジ時間 2,961 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i,n,N) for(int i=(n);i<(int)N;i++)
#define p(s) cout<<(s)<<endl
#define ck(n,a,b) ((a)<=(n)&&(n)<(b))
#define F first
#define S second
using namespace std;
const int inf=1e9;

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
bool visit[20][20];
int main(){
	int w,h;
	cin>>w>>h;
	string field[21];
	REP(i,0,h) cin>>field[i];
	stack<pair<int,int>> st;
	vector<pair<int,int>> vt[2];
	int v=0;
	REP(i,1,h-1){
		REP(j,1,w-1){
			if(field[i][j]=='.'&&!visit[i][j]){
				st.push({i,j});
				vt[v].push_back({i,j});
				visit[i][j]=true;
				while(!st.empty()){
					pair<int,int> now=st.top();st.pop();
					REP(i,0,4){
						int ny=now.F+dy[i],nx=now.S+dx[i];
						if(field[ny][nx]=='#') continue;
						if(visit[ny][nx]) continue;
						visit[ny][nx]=true;
						vt[v].push_back({ny,nx});
						st.push({ny,nx});
					}
				}
				v++;
			}
		}
		if(v==2) break;
	}
	int ans=inf;
	for(auto a : vt[0]){
		for(auto b :vt[1]){
			int x= abs(b.F-a.F),y=abs(b.S-a.S);
			ans=min(ans,x+y-1);
		}
	}
	p(ans);
	return 0;
}
0