結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
srup٩(๑`н´๑)۶
|
| 提出日時 | 2016-08-25 19:48:17 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,405 bytes |
| コンパイル時間 | 934 ms |
| コンパイル使用メモリ | 77,232 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-08 02:23:25 |
| 合計ジャッジ時間 | 1,739 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int dy[4] = {1, 0, 0, -1};
int dx[4] = {0, 1, -1, 0};
int w, h;
vector<string> s;
vector<pair<int, int> > ana[2];
void bfs(int f, int y, int x){
queue<pair<int, int> > q;
q.push(make_pair(y, x));
s[y][x] = '#'; ana[f].push_back(make_pair(y, x));
while(!q.empty()){
auto now = q.front(); q.pop();
rep(i, 4){
int nowy = dy[i] + now.first, nowx = dx[i] + now.second;
if(nowy < 0 && h <= nowy && nowx < 0 && w <= nowx) continue;
if(s[nowy][nowx] == '.'){
q.push(make_pair(nowy, nowx));
ana[f].push_back(make_pair(nowy, nowx));
s[nowy][nowx] = '#';
}
}
}
return;
}
int main(void){
cin >> w >> h;
rep(i, h){
string tmp; cin >> tmp;
s.push_back(tmp);
}
int cnt = 0;
rep(y, h){
rep(x, w){
if(s[y][x] == '.'){
if(cnt == 0){
bfs(0, y, x); cnt++;
}
else if(cnt == 1){
bfs(1, y, x);
}
}
}
}
rep(i, 2){
sort(ana[i].begin(), ana[i].end());
ana[i].erase(unique(ana[i].begin(), ana[i].end()), ana[i].end());//重複消去
}
int ans = 1e9;
for(auto u : ana[0]){
for(auto v : ana[1]){
// printf("%d\n", abs(u.first - v.first) + abs(u.second - v.second));
ans = min(ans, abs(u.first - v.first) + abs(u.second - v.second));
}
}
cout << ans - 1<< endl;
return 0;
}
srup٩(๑`н´๑)۶