結果

問題 No.157 2つの空洞
ユーザー matsukin1111matsukin1111
提出日時 2019-02-16 15:34:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,641 bytes
コンパイル時間 911 ms
コンパイル使用メモリ 100,136 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 06:33:10
合計ジャッジ時間 2,073 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cstdlib>  
#include <cmath>   
#include<cctype>
#include<string>
#include<set>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include <deque>
#include <utility> 

#define rep(i,m,n) for(int i = m;i < n;++i)
using namespace std;
using ll = long long;
using R = double;
const ll inf = 1LL << 50;
const ll MOD = 1e9 + 7;

string B[22];
int vis[22][22];
int dx[4] = { -1,0,1,0 }, dy[4] = {0,1,0,-1};
int w, h;
int cnt = 0;
vector< vector< pair<int, int> > >wh(2);




void check(int sx,int sy) {
	stack<pair<int, int>>st;
	vis[sy][sx] = 1;
	st.push({sx,sy});
	wh[cnt].push_back({sx,sy});

	while (!st.empty()) {
		int x, y;
		bool ok = 0;
		auto s = st.top();
		tie(x, y) = s;
		rep(d, 0, 4) {
			int xx = x + dx[d];
			int yy = y + dy[d];
			if (0 <= xx && xx <= w - 1 && 0 <= yy && yy <= h - 1) {
				if (vis[yy][xx] == 0 && B[yy][xx] != '#') {
					vis[yy][xx] = 1;
					st.push({xx,yy});
					wh[cnt].push_back({ xx,yy });
					ok = 1;
					break;
				}
			}
		}
		if (!ok) {
			st.pop();
		}

	}
	return;
}




int main() {
	cin >> w >> h;
	rep(i, 0, h)cin >> B[i];

	rep(i, 0, h) {
		rep(j, 0, w) {
			if (B[i][j] == '.' && vis[i][j] == 0 && cnt == 0) {
				check(j,i);
				cnt++;
			}
			else if (B[i][j] == '.' && vis[i][j] == 0 && cnt == 1) {
				check(j, i);
			}
		}
	}


	int ans = 101010;
	rep(i, 0, wh[0].size()) {
		rep(j, 0, wh[1].size()) {
			ans = min(abs(wh[0][i].first - wh[1][j].first) + abs(wh[0][i].second - wh[1][j].second)-1, ans);
		}
	}

	cout << ans << endl;

	return 0;
}

0