結果

問題 No.157 2つの空洞
ユーザー tottoripapertottoripaper
提出日時 2015-02-26 23:43:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,678 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 66,688 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 02:48:46
合計ジャッジ時間 1,423 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <queue>

#define mp std::make_pair

typedef std::pair<int,int> P;
typedef std::pair<int,P> Q;

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

int W, H;
std::string map[20];
int label[20][20];
bool used[20][20];

std::queue<Q> q;

void dfs(int y, int x, int l){
    if(!(0 <= y && y < H && 0 <= x && x < W)){return;}
    if(map[y][x] != '.'){return;}
    if(used[y][x]){return;}

    used[y][x] = true;
    if(l == 1){q.push(mp(0, mp(y, x)));}
    label[y][x] = l;
    for(int i=0;i<4;i++){
        dfs(y+dy[i], x+dx[i], l);
    }
}

int main(){
    std::cin >> W >> H;

    for(int i=0;i<H;i++){
        std::cin >> map[i];
    }

    {
        int x = 0;
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                if(map[i][j] == '.' && !used[i][j]){
                    x += 1;
                    dfs(i, j, x);
                }
            }
        }

        // for(int i=0;i<H;i++){
        //     for(int j=0;j<W;j++){
        //         std::cout << label[i][j] << ' ';
        //     }
        //     std::cout << std::endl;
        // }
    }

    memset(used, 0, sizeof(used));
    while(!q.empty()){
        Q p = q.front(); q.pop();
        int c = p.first, y = p.second.first, x = p.second.second;

        if(label[y][x] == 2){std::cout << c-1 << std::endl;; return 0;}

        for(int i=0;i<4;i++){
            int nx = x + dx[i], ny = y + dy[i];
            if(!(0 <= ny && ny < H && 0 <= nx && nx < W)){continue;}
            if(used[ny][nx]){continue;}

            used[ny][nx] = true;
            q.push(mp(c+1, mp(ny, nx)));
        }
    }
}

// 完全にスパゲッティ
0