結果

問題 No.157 2つの空洞
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-08-25 19:48:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,405 bytes
コンパイル時間 767 ms
コンパイル使用メモリ 78,844 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-07 20:59:01
合計ジャッジ時間 1,548 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int dy[4] = {1, 0, 0, -1};
int dx[4] = {0, 1, -1, 0};
int w, h; 
vector<string> s;
vector<pair<int, int> > ana[2];

void bfs(int f, int y, int x){
	queue<pair<int, int> > q;
	q.push(make_pair(y, x));
	s[y][x] = '#'; ana[f].push_back(make_pair(y, x));
	while(!q.empty()){
		auto now = q.front(); q.pop();
		rep(i, 4){
			int nowy = dy[i] + now.first, nowx = dx[i] + now.second;
			if(nowy < 0 && h <= nowy && nowx < 0 && w <= nowx) continue;
			if(s[nowy][nowx] == '.'){
				q.push(make_pair(nowy, nowx));
				ana[f].push_back(make_pair(nowy, nowx));
				s[nowy][nowx] = '#';
			}
		}
	}
	return;
}
int main(void){
	cin >> w >> h;
	rep(i, h){
		string tmp; cin >> tmp;
		s.push_back(tmp);
	}

	int cnt = 0;
	rep(y, h){
		rep(x, w){
			if(s[y][x] == '.'){
				if(cnt == 0){
					bfs(0, y, x); cnt++;
				}
				else if(cnt == 1){
					bfs(1, y, x);
				}
			}
		}
	}
	rep(i, 2){
		sort(ana[i].begin(), ana[i].end());
		ana[i].erase(unique(ana[i].begin(), ana[i].end()), ana[i].end());//重複消去
	}

	int ans = 1e9;
	for(auto u : ana[0]){
		for(auto v : ana[1]){
			// printf("%d\n", abs(u.first - v.first) + abs(u.second - v.second));
			ans = min(ans, abs(u.first - v.first) + abs(u.second - v.second));
		}
	}
	cout << ans - 1<< endl;
	return 0;
}
0