結果

問題 No.157 2つの空洞
コンテスト
ユーザー Rumain831
提出日時 2026-09-21 04:52:19
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 2,000 ms
+ 341µs
コード長 1,130 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,542 ms
コンパイル使用メモリ 210,748 KB
実行使用メモリ 10,044 KB
最終ジャッジ日時 2026-09-21 04:52:26
合計ジャッジ時間 3,271 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<iostream>
#include<vector>
#include<queue>
#include<tuple>
#include<algorithm>
using namespace std;
using ll = long long;
using tu = tuple<int, int, int>;

int main(void){
  int w, h; cin >> w >> h;
  vector<string> s(h);
  for(auto&x:s) cin >> x;
  deque<tu> bfs;
  vector dist(h, vector<int>(w, 1e9));
  for(int i=0; i<h; i++)for(int j=0; j<w; j++)if(bfs.empty()&&s[i][j]=='.'){
    bfs.emplace_front(0, i, j);
    dist[i][j]=0; break;
  }
  int di[4]={-1, 0, 1, 0};
  int dj[4]={0, 1, 0, -1};
  while(bfs.size()){
    auto [d, i, j]=bfs.front(); bfs.pop_front();
    if(dist[i][j]!=d) continue;
    for(int k=0; k<4; k++){
      int ni=i+di[k], nj=j+dj[k];
      if(ni<0||nj<0||ni>=h||nj>=w) continue;
      int nd=d+(s[ni][nj]=='.'?0:1);
      if(dist[ni][nj]<=nd) continue;
      dist[ni][nj]=nd;
      if(s[ni][nj]=='.') bfs.emplace_front(nd, ni, nj);
      else bfs.emplace_back(nd, ni, nj);
    }
  }
  int ans=1e9;
  for(int i=0; i<h; i++)for(int j=0; j<w; j++){
    //cout << dist[i][j] << (j==w-1?'\n':' ');
    if(s[i][j]=='.'&&dist[i][j]>0) ans=min(ans, dist[i][j]);
  }
  cout << ans << endl;
  return 0; 
}
0