結果

問題 No.157 2つの空洞
ユーザー kkslucy1kkslucy1
提出日時 2018-03-13 23:14:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,049 bytes
コンパイル時間 2,021 ms
コンパイル使用メモリ 150,988 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 21:31:49
合計ジャッジ時間 2,206 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 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 <bits/stdc++.h>
using namespace std;

int w, h;
vector<vector<int>> c;
void replace(int i, int j, int n) {
	if (c[i][j] == 1) {
		c[i][j] = n;
		replace(i + 1, j, n);
		replace(i - 1, j, n);
		replace(i, j + 1, n);
		replace(i, j - 1, n);
	}
	return;
}
int main() {
	cin >> w >> h;
	c.resize(h);
	for (int i = 0;i < h;i++) {
		string s;
		cin >> s;
		c[i].resize(w);
		for (int j = 0;j < w;j++) {
			if (s[j] == '#') {
				c[i][j] = 0;
			}
			else {
				c[i][j] = 1;
			}
		}
	}

	int count = 0;
	for (int i = 1;i < h-1;i++) {
		for (int j = 1;j < w-1;j++) {
			if (c[i][j] == 1) {
				replace(i, j, count + 2);
				count++;
			}
			if (count==2)
				break;
		}
		if (count == 2)
			break;
	}
	int mi = 20 * 20;
	for(int i=1;i<h-1;i++){
		for (int j = 1;j < w - 1;j++) {
			if (c[i][j] == 2) {
				for (int i2 = 1;i2 < h - 1;i2++) {
					for (int j2 = 1;j2 < w - 1;j2++) {
						if (c[i2][j2] == 3) {
							int d = abs(i - i2) + abs(j - j2)-1;
							mi = min(mi, d);
						}
					}
				}
			}
		}
	}
	cout << mi << endl;




	return 0;
}
0