結果

問題 No.157 2つの空洞
ユーザー なおなお
提出日時 2015-02-26 23:45:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,803 bytes
コンパイル時間 2,185 ms
コンパイル使用メモリ 154,800 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 02:51:25
合計ジャッジ時間 3,071 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))
#define FOR(i, f, t)        for(int(i)=(f);(i)<(t);(++i))
#define RREP(i, n)          for(int(i)=(n)-1;(i)>=0;--(i))
const int MOD = int(1e9+7);

int N,M,W,H;
int res = 0;
int c[111111];

const int dir[][2] = {{1,0},{0,1},{-1,0},{0,-1},{1,-1},{1,1},{-1,1},{-1,-1}};

int main(){
    do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0);
    cin >> W >> H;

    vector<string> v;
    REP(i,H){
        string s;
        cin >> s;
        v.push_back(s);
    }

    int k = 0;
    vector<pair<int,int>> kk[2];
    REP(y,H)REP(x,W){
        if(v[y][x] == '.'){
            deque<pair<int,int> > q;
            q.push_back(make_pair(x,y));
            kk[k].push_back(make_pair(x,y));
            v[y][x] = '#';
            while(!q.empty()){
                int xx = q.front().first;
                int yy = q.front().second;
                q.pop_front();
                REP(i,4){
                    int mx = xx + dir[i][0];
                    int my = yy + dir[i][1];
                    if(mx < 0 || my < 0 || mx >= W || my >= H) continue;
                    if(v[my][mx] == '#') continue;
                    q.push_back(make_pair(mx,my));
                    kk[k].push_back(make_pair(mx,my));
                    v[my][mx] = '#';
                }
            }
            k++;
        }
    }
    int minv = 99999;
    int s0 = kk[0].size();
    int s1 = kk[1].size();
    REP(i0,s0) REP(i1,s1){
        int dif = abs(kk[0][i0].first - kk[1][i1].first) + abs(kk[0][i0].second - kk[1][i1].second);
        minv = min(minv, dif);
    }


    cout << minv-1 << endl;
    return 0;
}
0