結果

問題 No.157 2つの空洞
ユーザー cielciel
提出日時 2015-02-27 01:36:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 994 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 79,724 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 03:21:24
合計ジャッジ時間 1,905 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <queue>
#include <set>
#include <cstdlib>
using namespace std;
typedef pair<int,int> pii;
typedef struct{
	int x;
	int y;
}dir;
dir D[]={
	{0,-1},{0,1},
	{-1,0},{1,0},
};
int manhattan(const pii &a,const pii &b){
	return abs(a.first-b.first)+abs(a.second-b.second);
}
int main(){
	int W,H;
	cin>>W>>H;
	vector<string>v(H);
	for(int i=0;i<H;i++)cin>>v[i];
	vector<set<pii> >lst;
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)if(v[i][j]=='.'){
		v[i][j]='#';
		set<pii> se={{i,j}};
		queue<pii> q;
		for(q.push({i,j});!q.empty();){
			pii cur=q.front();q.pop();
			for(auto &d:D){
				pii nxt={cur.first+d.y,cur.second+d.x};
				if(0<=nxt.first&&nxt.first<H && 0<=nxt.second&&nxt.second<W && v[nxt.first][nxt.second]=='.'){
					v[nxt.first][nxt.second]='#';
					se.insert(nxt);
					q.push(nxt);
				}
			}
		}
		lst.emplace_back(se);
	}
	int r=1<<29;
	for(const pii &e:lst[0])for(const pii &f:lst[1]){
		r=min(r,manhattan(e,f));
	}
	cout<<r-1<<endl;
}
0