結果

問題 No.157 2つの空洞
コンテスト
ユーザー mamekin
提出日時 2015-03-03 21:40:56
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,878 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 652 ms
コンパイル使用メモリ 122,060 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-03-09 11:16:54
合計ジャッジ時間 1,208 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:44:22: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
   44 |     q1.push(make_pair(sy, sx));
      |             ~~~~~~~~~^~~~~~~~
main.cpp:32:9: note: 'sy' was declared here
   32 |     int sy, sx;
      |         ^~
main.cpp:44:22: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
   44 |     q1.push(make_pair(sy, sx));
      |             ~~~~~~~~~^~~~~~~~
main.cpp:32:13: note: 'sx' was declared here
   32 |     int sy, sx;
      |             ^~

ソースコード

diff #
raw source code

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

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

int main()
{
    int w, h;
    cin >> w >> h;

    vector<string> c(h+2, string(w+2, '!'));
    int sy, sx;
    for(int y=1; y<=h; ++y){
        for(int x=1; x<=w; ++x){
            cin >> c[y][x];
            if(c[y][x] == '.'){
                sy = y;
                sx = x;
            }
        }
    }

    queue<pair<int, int> > q1, q2;
    q1.push(make_pair(sy, sx));
    q2.push(make_pair(sy, sx));
    c[sy][sx] = '!';
    while(!q1.empty()){
        int y = q1.front().first;
        int x = q1.front().second;
        q1.pop();

        for(int i=0; i<4; ++i){
            int y2 = y + dy[i];
            int x2 = x + dx[i];
            if(c[y2][x2] == '.'){
                q1.push(make_pair(y2, x2));
                q2.push(make_pair(y2, x2));
                c[y2][x2] = '!';
            }
        }
    }

    int ret = 0;
    for(;;){
        int n = q2.size();
        while(--n >= 0){
            int y = q2.front().first;
            int x = q2.front().second;
            q2.pop();

            for(int i=0; i<4; ++i){
                int y2 = y + dy[i];
                int x2 = x + dx[i];
                if(c[y2][x2] == '.'){
                    cout << ret << endl;
                    return 0;
                }
                if(c[y2][x2] == '#'){
                    q2.push(make_pair(y2, x2));
                    c[y2][x2] = '!';
                }
            }
        }
        ++ ret;
    }
}
0