結果

問題 No.157 2つの空洞
ユーザー oxyshoweroxyshower
提出日時 2020-01-12 14:06:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,544 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 176,492 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 09:36:29
合計ジャッジ時間 3,138 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 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>
using namespace std;
#ifdef LOCAL_DEBUG
  #include "LOCAL_DEBUG.hpp"
#endif
#define int long long
template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template<class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
    t = v;
}
template<class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v){
    for (auto &e : t) fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);
const int INF = 1LL << 60;

signed main(){

  int h, w; cin >> w >> h;
  auto c = make_vec<char>(h, w);
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      cin >> c[i][j];
    }
  }

  auto group = make_vec<int>(h, w);
  fill(group, -1);
  int id = 1;
  function< void(int,int) > dfs =
  [&](int y, int x){
    int dx[4] = {1,0,-1,0}, dy[4] = {0,-1,0,1};
    for(int i = 0; i < 4; i++){
      int ny = y + dy[i];
      int nx = x + dx[i];
      if(ny < 0 || h <= ny || nx < 0 || w <= nx) continue;
      if(c[ny][nx] == '.' && group[ny][nx] == -1){
        group[ny][nx] = id;
        dfs(ny, nx);
      }
    }
  };

  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(c[i][j] == '.' && group[i][j] == -1){
        group[i][j] = id;
        dfs(i, j);
        id++;
      }
    }
  }

  auto dp = make_vec<int>(h, w);
  fill(dp, INF);
  function< void(int,int) > dfs2 =
  [&](int y, int x){
    int dx[4] = {1,0,-1,0}, dy[4] = {0,-1,0,1};
    for(int i = 0; i < 4; i++){
      int ny = y + dy[i];
      int nx = x + dx[i];
      if(ny < 0 || h <= ny || nx < 0 || w <= nx) continue;
      if(c[ny][nx] == '.'){
        if(group[ny][nx] == 1){
          if(dp[ny][nx] != 0){
            dp[ny][nx] = 0;
            dfs2(ny, nx);
          }
        }else{
          if(dp[ny][nx] > dp[y][x] + 1){
            dp[ny][nx] = dp[y][x] + 1;
            dfs2(ny, nx);
          }
        }
      }else{
        if(dp[ny][nx] > dp[y][x] + 1){
          dp[ny][nx] = dp[y][x] + 1;
          dfs2(ny, nx);
        }
      }
    }
  };

  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(group[i][j] == 1){
        dp[i][j] = 0;
        dfs2(i, j);
      }
    }
  }

  int ans = INF;
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(group[i][j] == 2){
        ans = min(ans, dp[i][j]);
      }
    }
  }
  cout << ans-1 << endl;

  return 0;
}
0