結果

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

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> pint;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
const int INF = 100000000;
const int dx[4]={0,1,0,-1}, dy[4]={-1,0,1,0};
struct P{int x;int y;P(int X=0,int Y=0){x=X;y=Y;}};

int W,H;
char c[22][22];
vector<P> cave1;
vector<P> cave2;

bool sec = false;
void dfs(int x, int y) {
	c[y][x] = '#';
	if(!sec) cave1.push_back(P(x,y));
	else  cave2.push_back(P(x,y));

	rep(i,4) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx >= 0 && nx < W && ny >= 0 && ny < H) {
			if(c[ny][nx] == '.') dfs(nx, ny);
		}
	}
	return;
}

int main() {
	cin >> W >> H;
	rep(i,H) {
		scanf("%s", c[i]);
	}

	rep(y,H) {
		rep(x,W) {
			if(c[y][x] == '.') {
				dfs(x, y);
				sec = true;
			}
		}
	}

	int ans=INF;
	int siz1 = cave1.size();
	int siz2 = cave2.size();
	rep(i,siz1) {
		rep(j,siz2) {
			ans = min(ans, abs(cave1[i].x - cave2[j].x) + abs(cave1[i].y - cave2[j].y));
		}
	}
	printf("%d\n",ans-1);
	return 0;
}
0