結果

問題 No.157 2つの空洞
コンテスト
ユーザー iqueue02
提出日時 2019-11-04 17:43:48
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,130 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,134 ms
コンパイル使用メモリ 192,720 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-04 15:51:11
合計ジャッジ時間 1,784 ms
ジャッジサーバーID
(参考情報)
judge5_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:64,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/algorithm:62,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:53,
                 from main.cpp:1:
In constructor 'constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = int&; _U2 = int&; typename std::enable_if<(std::_PCC<true, _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<true, _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> = true; _T1 = int; _T2 = int]',
    inlined from 'int main()' at main.cpp:25:17:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_pair.h:902:11: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
  902 |         : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y))
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:17:7: note: 'sy' was declared here
   17 |   int sy, sx;
      |       ^~
In constructor 'constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = int&; _U2 = int&; typename std::enable_if<(std::_PCC<true, _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<true, _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> = true; _T1 = int; _T2 = int]',
    inlined from 'int main()' at main.cpp:25:17:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_pair.h:902:42: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
  902 |         : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y))
      |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:17:11: note: 'sx' was declared here
   17 |   int sy, sx;
      |           ^~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

const int INF = 1e9;

int dy[] = {1,0,-1,0};
int dx[] = {0,1,0,-1};

int main(){
  int h, w;
  cin >> w >> h;
  vector<vector<char>> g(h,vector<char>(w));
  int sy, sx;
  rep(i,h) rep(j,w) {
    cin >> g[i][j];
    if (g[i][j] == '.') sy = i, sx = j;
  }
  vector<vector<int>> dist(h,vector<int>(w,INF));
  int res = INF;
  deque<P> deq;
  deq.push_front({sy,sx});
  dist[sy][sx] = 0;
  while (!deq.empty()) {
    P p = deq.front(); deq.pop_front();  
    rep(i,4) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if (ny < 0 || nx < 0 || nx >= w-1 || ny >= h-1) continue;
      if (dist[ny][nx] != INF) continue;
      if (g[ny][nx] == '.') {
        if (g[p.first][p.second] == '#') res = min(res,dist[p.first][p.second]);
        dist[ny][nx] = 0;
        deq.push_front({ny,nx});
      } else {
        dist[ny][nx] = dist[p.first][p.second] + 1;
        deq.push_back({ny,nx});
      }
    }
  }
  
  cout << res << endl;
  return 0;
}
0