結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-09-27 01:37:33 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,491 bytes |
| コンパイル時間 | 882 ms |
| コンパイル使用メモリ | 80,884 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-15 16:55:17 |
| 合計ジャッジ時間 | 1,702 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#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;
}