結果

問題 No.157 2つの空洞
ユーザー zxcv_13kzxcv_13k
提出日時 2015-02-27 00:35:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 922 bytes
コンパイル時間 431 ms
コンパイル使用メモリ 52,404 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 03:03:14
合計ジャッジ時間 1,369 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdlib>
using namespace std;

int W, H;
char MAP[20][21];
int aa[20][20] = {0};

void dfs(int a, int b) {

	int dx[4] = {1, -1, 0, 0};
	int dy[4] = {0, 0, 1, -1};
	
	aa[a][b] = 1;
	
	for(int i = 0; i < 4; i++) {
		if(!aa[a+dx[i]][b+dy[i]] && MAP[a+dx[i]][b+dy[i]]== '.')
			dfs(a+dx[i], b+dy[i]);
	}
	
}


int main() {

	cin >> W >> H;
	
	for(int i = 0; i < H; i++) {
		cin >> MAP[i];
	}
	
	bool end = false;
	for(int i = 0; i < H; i++) {
		for(int j = 0; j < W; j++) {
			if(MAP[i][j] == '.') {
				dfs(i, j);
				end = true;
				break;
			}
		}
		if(end) break;
	}
	
	int ans = 1001001001;
	for(int i = 0; i < H; i++) {
		for(int j = 0; j < W; j++) {
			if(!aa[i][j]) continue;
			for(int k = 0; k < H; k++) {
				for(int l = 0; l < W; l++) {
					if(!aa[k][l] && MAP[k][l] == '.') {
						ans = min(ans, abs(i-k)+abs(j-l)-1);
					}
				}
			}
		}
	}
	
	cout << ans << endl;

	return 0;
}
0