結果

問題 No.157 2つの空洞
ユーザー daleksprinterdaleksprinter
提出日時 2018-09-02 17:20:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,325 bytes
コンパイル時間 729 ms
コンパイル使用メモリ 82,836 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 20:24:59
合計ジャッジ時間 1,450 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <numeric>
#include <string>
using namespace std;

#define rep(i,a,b) for(int i=a;i<b;i++)
const int inf = 100100100;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

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

    char c[h][w];

    rep(i,0,h) rep(j,0,w) cin >> c[i][j];

    vector< vector<pair<int,int>> > arr;

    rep(i,0,h) rep(j,0,w){
        if(c[i][j] == '.'){
            vector<pair<int,int>> tmp;
            vector<pair<int,int>> stk;
            stk.push_back(make_pair(i,j));
            while(!stk.empty()){
                pair<int,int> t = stk.back();
                stk.pop_back();
                c[t.first][t.second] = '#';
                tmp.push_back(make_pair(t.first,t.second));
                rep(k,0,4){
                    int y = t.first + dy[k]; int x = t.second + dx[k];

                    if(-1 < y && y < h && -1 < x && x < w && c[y][x] == '.'){
                        stk.push_back(make_pair(y,x));
                    }

                }
            }

            arr.push_back(tmp);


        }
    }
    int ans = inf;

    for(auto a: arr[0]){
        for(auto b: arr[1]){
            ans = min(ans,abs(a.first - b.first) + abs(a.second - b.second));
        }
    }
    cout << ans-1 << endl;

}

0