結果

問題 No.157 2つの空洞
ユーザー nanophoto12nanophoto12
提出日時 2016-02-19 22:33:28
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,749 bytes
コンパイル時間 748 ms
コンパイル使用メモリ 77,512 KB
実行使用メモリ 814,392 KB
最終ジャッジ日時 2023-10-23 18:34:41
合計ジャッジ時間 5,271 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <queue>
#include <algorithm>
#include <vector>
#include <iostream>
#include <numeric>
#include <tuple>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define ALL(container) (container).begin(), (container).end()

pii shifts[] = {{1,0},{0,1},{-1,0},{0,-1}};

int f[200][200];
int visit[200][200];
vector<pii> vs[3];

int main(int argc, char *argv[]){
    ios::sync_with_stdio(false);
    int w,h;
    cin >> w >> h;
    REP(i, 200) REP(j, 200) f[i][j] = 0;
    
    REP(i, h)
    {
        string s;
        cin >> s;
        REP(j, w)
        {
            if(s[j] == '.')
            {
                f[i + 1][j + 1] = -1;
            }
        }
    }

    int id = 1;
    REP(i, h) REP(j, w)
    {
        int sh = i + 1;
        int sw = j + 1;
        if(f[sh][sw] >= 0)
        {
            continue;
        }
        queue<tiii> q;
        q.push(make_tuple(sw,sh, id));
        vs[id].emplace_back(sw,sh);
        while(!q.empty())
        {
            const auto t = q.front();
            q.pop();
            for(int s = 0;s < 4;s++)
            {
                auto x = get<0>(t) + shifts[s].first;
                auto y = get<1>(t) + shifts[s].second;
                if(f[y][x] >= 0)
                {
                    continue;
                }
                f[y][x] = id;
                vs[id].emplace_back(x,y);
                q.push(make_tuple(x,y,id));
            }
        }
        id++;
    }
    {
        REP(i, 200) REP(j, 200) visit[i][j] = 0;
        queue<tiii> q;
        for(int i = 0;i < vs[1].size();i++)
        {
            auto t = vs[1][i];
            for(int s = 0;s < 4;s++)
            {
                auto x = get<0>(t) + shifts[s].first;
                auto y = get<1>(t) + shifts[s].second;
                if(f[y][x] == 0)
                {
                    q.push(make_tuple(x,y,1));
                    visit[y][x] = true;
                }
            }
        }
        while(!q.empty())
        {
            const auto t = q.front();
            q.pop();
            auto c = get<2>(t);
            for(int s = 0;s < 4;s++)
            {
                auto x = get<0>(t) + shifts[s].first;
                auto y = get<1>(t) + shifts[s].second;
                if(f[y][x] == 0)
                {
                    if(!visit[y][x])
                    {
                        q.push(make_tuple(x,y,c+1));
                    }
                }
                if(f[y][x] == 2)
                {
                    cout << c << endl;
                    return 0;
                }
            }
        }
    }
    return 0;
}
0