結果

問題 No.157 2つの空洞
ユーザー 41Toame41Toame
提出日時 2018-09-08 15:13:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,366 bytes
コンパイル時間 1,453 ms
コンパイル使用メモリ 147,916 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-22 10:24:51
合計ジャッジ時間 2,379 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000


int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };

char c[25][25];
typedef pair<int, int> P;
vector<P> v1;
vector<P> v2;

void dfs(int num, int x, int y) {
	//cout << x << " " << y << endl;
	if (num == 0) v1.push_back({ x, y });
	else v2.push_back({ x, y });
	c[y][x] = '#';
	rep(i, 4) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		if (c[yy][xx] == '.') dfs(num, xx, yy);
	}
}


int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
	int W, H;
	cin >> W >> H;
	rep(i, H) rep(j, W) cin >> c[i][j];
	int i = 0;
	int num = 0;
	while (num < 2) {
		int j = 0;
		while (j < W) {
			//cout << i << " " << j << endl;
			if (c[i][j] == '.') {
				dfs(num, j, i);
				num++;
			}
			j++;
			if (num == 2) break;
		}
		i++;
	}
	int ans = 1000;
	rep(i, v1.size()) {
		rep(j, v2.size()) {
			int dx = abs(v1[i].first - v2[j].first);
			int dy = abs(v1[i].second - v2[j].second);
			ans = min(ans, dx + dy);
		}
	}
	cout << ans - 1<< endl;
	


    return 0;
}
0