結果

問題 No.157 2つの空洞
ユーザー alpha_virginisalpha_virginis
提出日時 2015-07-18 10:07:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,064 bytes
コンパイル時間 1,245 ms
コンパイル使用メモリ 145,372 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 18:55:36
合計ジャッジ時間 2,041 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

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