結果

問題 No.157 2つの空洞
ユーザー kk
提出日時 2020-07-07 07:41:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 836 bytes
コンパイル時間 2,016 ms
コンパイル使用メモリ 202,788 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-25 10:10:02
合計ジャッジ時間 3,091 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

int w, h;
string bd[20];
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};

void go(int y, int x, char c) {
  if (bd[y][x] != '.') return;
  bd[y][x] = c;
  REP (k, 4) {
    int yy = y + dy[k];
    int xx = x + dx[k];
    if (yy < 0 || yy >= h) continue;
    if (xx < 0 || xx >= w) continue;
    go(yy, xx, c);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> w >> h;
  REP (i, h) cin >> bd[i];

  char c = 'X';  
  REP (i, h) REP (j, w) {
    if (bd[i][j] == '.')
      go(i, j, c++);
  }

  int ret = 1000000;
  REP (y1, h) REP (x1, w) REP (y2, h) REP (x2, w) {
    if (bd[y1][x1] == 'X' && bd[y2][x2] == 'Y')
      ret = min(ret, abs(x1 - x2) + abs(y1 - y2) - 1);
  }
  cout << ret << endl;
  
  return 0;
}
0