結果

問題 No.157 2つの空洞
ユーザー komori3komori3
提出日時 2017-09-27 01:37:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,491 bytes
コンパイル時間 634 ms
コンパイル使用メモリ 80,232 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-09 19:45:25
合計ジャッジ時間 1,643 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <functional>
#include <set>
#include <numeric>
#include <stack>
#include <utility>
#include <time.h>
//#include "util.h"

using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;

template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const pair<_KTy, _Ty>& m) { cout << "{" << m.first << ", " << m.second << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const vector<_Ty>& v) { if (v.empty()) { cout << "{ }"; return ostr; }	cout << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { cout << ", " << *itr; }	cout << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const set<_Ty>& s) { if (s.empty()) { cout << "{ }"; return ostr; } cout << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { cout << ", " << *itr; }	cout << "}"; return ostr; }

#define PI 3.14159265358979323846
#define EPS 1e-6
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define all(x) (x).begin(), (x).end()

typedef pair<int, int> Pii;

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

void rec(vector<string>& mp, vector<Pii>& v, int x, int y) {
	if (mp[y][x] == '#') return;
	
	// 空洞座標に追加
	v.push_back(Pii(x, y));
	// # で塗りつぶす
	mp[y][x] = '#';
	// 4 方向探索
	for (int dir = 0; dir < 4; dir++)
		rec(mp, v, x + dx[dir], y + dy[dir]);
}

int md(Pii p, Pii q)
{
	return abs(p.first - q.first) + abs(p.second - q.second);
}

int yuki0157() 
{
	int W, H, label;
	cin >> W >> H;
	vector<string> mp(H);
	for (int i = 0; i < H; i++)
		cin >> mp[i];

	// 空洞 0, 1 に属する点の集合
	vector<Pii> v[2];

	label = 0; // 空洞番号
	for (int y = 1; y <= H - 2; y++) {
		for (int x = 1; x <= W - 2; x++) {
			if (mp[y][x] == '#') continue;
			
			rec(mp, v[label], x, y);

			label++;
		}
	}
	
	// min(Manhattan dist(v[0][i], v[1][j]) - 1) を求める
	int d, ans = W + H;
	for (int i = 0; i < v[0].size(); i++) {
		for (int j = 0; j < v[1].size(); j++) {
			d = md(v[0][i], v[1][j]) - 1;
			if (ans > d) ans = d;
		}
	}

	cout << ans << endl;

	return 0;
}

int main()
{
	//clock_t start, end;
	//start = clock();

	yuki0157();

	//end = clock();
	//printf("%d msec.\n", end - start);

	return 0;
}
0