結果

問題 No.157 2つの空洞
ユーザー ldsybldsyb
提出日時 2016-12-03 18:42:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,333 bytes
コンパイル時間 1,423 ms
コンパイル使用メモリ 79,452 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 12:52:36
合計ジャッジ時間 2,022 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

vector<pair<int, int>> bfs(vector<string>& s){
	int dd[] = {0, 1, 0, -1, 0};
	stack<pair<int, int>> st;
	vector<pair<int, int>> r;
	for(int i = 0; i < s.size(); i++) for(int j = 0; j < s[0].size(); j++) if(s[i][j] == '.'){
		st.push(make_pair(i, j));
		r.emplace_back(i, j);
		s[i][j] = '#';
		i = s.size(); j = s[0].size();
	}
	while(!st.empty()){
		pair<int, int> now = st.top(); st.pop();
		for(int i = 0; i < 4; i++) if(0 <= now.first + dd[i] && now.first + dd[i] < s.size() && 0 <= now.second + dd[i + 1] && now.second + dd[i + 1] < s[0].size() && s[now.first + dd[i]][now.second + dd[i + 1]] == '.'){
			s[now.first + dd[i]][now.second + dd[i + 1]] = '#';
			st.push(make_pair(now.first + dd[i], now.second + dd[i + 1]));
			r.emplace_back(now.first + dd[i], now.second + dd[i + 1]);
		}
	}
	return r;
}

int main(){
	int w, h;
	cin >> w >> h;
	vector<string> s(h);
	for(auto& ss:s) cin >> ss;
	vector<pair<int, int>> a = bfs(s), b = bfs(s);
	int ans = 1e9;
	for(int i = 0; i < a.size(); i++) for(int j = 0; j < b.size(); j++) ans = min(ans,(a[i].first - b[j].first >= 0 ? a[i].first - b[j].first : -(a[i].first - b[j].first)) + (a[i].second - b[j].second >= 0 ? a[i].second - b[j].second : -(a[i].second - b[j].second)) - 1);
	cout << ans << endl;
}
0