結果

問題 No.157 2つの空洞
ユーザー hotpepsihotpepsi
提出日時 2015-09-23 23:27:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 995 bytes
コンパイル時間 765 ms
コンパイル使用メモリ 62,480 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 14:46:13
合計ジャッジ時間 2,082 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>

using namespace std;

int W, H;
string b[32];

void fill(int x, int y) {
	b[y][x] = '-';
	int dx[] = { -1, 1, 0, 0 };
	int dy[] = { 0, 0, -1, 1 };
	for (int d = 0; d < 4; ++d) {
		int nx = x + dx[d], ny = y + dy[d];
		if (nx >= 0 && nx < W && ny >= 0 && ny < H) {
			if (b[ny][nx] == '.') {
				fill(nx, ny);
			}
		}
	}
}

int main(int argc, char *argv[])
{
	string s;
	getline(cin, s);
	stringstream ss(s);
	ss >> W >> H;
	for (int i = 0; i < H; ++i) {
		getline(cin, b[i]);
	}
	for (int i = 0; i < H; ++i) {
		int pos = b[i].find('.');
		if (pos >= 0) {
			fill(pos, i);
			break;
		}
	}
	int ans = 1 << 30;
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			if (b[i][j] == '-') {
				for (int y = 0; y < H; ++y) {
					for (int x = 0; x < W; ++x) {
						if (b[y][x] == '.') {
							ans = min(ans, abs(x - j) + abs(y - i) - 1);
						}
					}
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}
0