結果

問題 No.157 2つの空洞
ユーザー face4
提出日時 2018-05-24 09:15:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 852 bytes
コンパイル時間 648 ms
コンパイル使用メモリ 70,644 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-28 16:43:18
合計ジャッジ時間 1,561 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

// coding on smartphone
#include<iostream>
#include<vector>
using namespace std;

vector<pair<int, int>> hole;
int w, h;
char mat[20][20];

void bfs(int i, int j){
	if(i < 0 || i > h || j < 0 || j > w)	return;
	
	if(mat[i][j] == '.'){
		hole.push_back({i, j});
		mat[i][j] = '#';
		bfs(i-1, j);
		bfs(i+1, j);
		bfs(i, j+1);
		bfs(i, j-1);
	}
}

int main(){
	cin >> w >> h;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			cin >> mat[i][j];
		}
	}
	
	bool found = false;
	int ans = 50;
	
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(mat[i][j] == '.'){
				if(!found){
					 bfs(i, j);
					 found = true;
				}else{
					for(int k = 0; k < hole.size(); k++){	
						int tmp = abs(i-hole[k].first) + abs(j-hole[k].second);
						ans = min(tmp, ans);
					}
				}
			}
		}
	}
	
	ans--;
	cout << ans << endl;
	return 0;
}
0