結果

問題 No.157 2つの空洞
ユーザー face4face4
提出日時 2018-05-24 09:15:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 852 bytes
コンパイル時間 579 ms
コンパイル使用メモリ 70,136 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-11 02:12:18
合計ジャッジ時間 2,267 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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