結果

問題 No.157 2つの空洞
ユーザー knewknowlknewknowl
提出日時 2015-03-02 22:06:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 901 bytes
コンパイル時間 496 ms
コンパイル使用メモリ 58,440 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 06:32:19
合計ジャッジ時間 1,561 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cmath>
using namespace std;
int w,h;
char tbl[21][21];
int t[21][21];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
void dfs(int x,int y,int c){
  t[y][x]=c;
  for(int i=0;i<4;++i){
    int mx=x+dx[i],my=y+dy[i];
    if(mx>=w||my>=h||mx<0||my<0) continue;
    if(!t[my][mx]&&tbl[my][mx]=='.') dfs(mx,my,c);
  }
}
int calc(int x,int y){
  int ret = 1<<30;
  for(int i=0;i<h;++i){
    for(int j=0;j<w;++j){
      if(t[i][j]==2){
	ret = min(ret,abs(x-j)+abs(y-i));
      }
    }
  }
  return ret;
}
int main(){
  cin >> w >> h;
  for(int i=0;i<h;++i) cin >> tbl[i];
  int c = 1;
  for(int i=0;i<h;++i){
    for(int j=0;j<w;++j){
      if(tbl[i][j]=='.'&&!t[i][j]){
	dfs(j,i,c);
	++c;
      }
    }
  }

  int ans = 1<<30;
  for(int i=0;i<h;++i){
    for(int j=0;j<w;++j){
      if(t[i][j]==1){
	ans = min(ans,calc(j,i)-1);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
0