結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-08-25 16:49:15 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,187 bytes |
| コンパイル時間 | 646 ms |
| コンパイル使用メモリ | 73,656 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-08 02:22:37 |
| 合計ジャッジ時間 | 1,389 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
int W,H;
cin>>W>>H;
int C[22][22] = {};
for(int i=1;i<=H;i++){
string str;
cin>>str;
for(int j=1;j<=W;j++){
if(str[j]=='.'){
C[i][j] = 1;
}
}
}
queue<pair<int,int> > que;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
for(int i=1;i<=H;i++){
for(int j=1;j<=W;j++){
if(C[i][j] == 1){
que.push(make_pair(i,j));
C[i][j] = 2;
while(!que.empty()){
int y = que.front().first;
int x = que.front().second;
que.pop();
for(int k=0;k<4;k++){
if(C[dy[k]+y][dx[k]+x]==1){
C[dy[k]+y][dx[k]+x] = 2;
que.push(make_pair(dy[k]+y,dx[k]+x));
}
}
} int res = 1000000;
for(int i1=1;i1<=H;i1++){
for(int i2=1;i2<=W;i2++){
if(C[i1][i2]==1){
for(int j1=1;j1<=H;j1++){
for(int j2=1;j2<=W;j2++){
if(C[j1][j2]==2){
//cout<<i1<<","<<i2<<","<<j1<<","<<j2<<endl;
res = min(res,abs(j1-i1) + abs(j2-i2)-1);
//cout<<res<<endl;
}
}
}
}
}
}
cout<<res<<endl;
return 0;
}
}
}
}