結果

問題 No.157 2つの空洞
ユーザー alpha_virginis
提出日時 2015-07-18 10:07:14
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,064 bytes
コンパイル時間 1,154 ms
コンパイル使用メモリ 160,680 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-08 10:07:27
合計ジャッジ時間 1,796 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int W, H;
std::string str[32];

void dfs(int x, int y) {
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};

  str[y][x] = '1';
  for(int i = 0; i < 4; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if( 0 <= nx and nx < W and 0 <= ny and ny < H ) {
      if( str[ny][nx] == '.' ) {
        str[ny][nx] = '1';
        dfs(nx, ny);
      }
    }
  }

  return;
}
  

int main() {


  std::cin >> W >> H;
  for(int i = 0; i < H; ++i) {
    std::cin >> str[i];
  }

  

  for(int i = 0; i < H; ++i) {
    for(int j = 0; j < W; ++j) {
      if( str[i][j] == '.' ) {
        dfs(j, i);
        goto label_1;
      }
    }
  }
 label_1:;

  int ans = 100000;  
  for(int x1 = 0; x1 < W; ++x1) {
    for(int y1 = 0; y1 < H; ++y1) {
      for(int x2 = 0; x2 < W; ++x2) {
        for(int y2 = 0; y2 < H; ++y2) {
          if( str[y1][x1] == '.' and str[y2][x2] == '1' ) {
            ans = std::min(ans, abs(x2 - x1) + abs(y2 - y1) - 1);
          }
        }
      }
    }
  }

  std::cout << ans << std::endl;
  
  return 0;
}
0