結果

問題 No.157 2つの空洞
ユーザー phsplsphspls
提出日時 2020-04-08 23:11:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,023 bytes
コンパイル時間 1,806 ms
コンパイル使用メモリ 169,752 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-26 11:08:16
合計ジャッジ時間 2,761 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define llong long long

vector<pair<int, int>> pool1;
vector<pair<int, int>> pool2;
int w, h;

void dfs(vector<string> &mapping, int y, int x, bool ispool1) {
    if(!(0 <= y && y < h && 0 <= x && x < w)) return;
    if(mapping[y][x] == '#') return;
    mapping[y][x] = '#';
    if(ispool1) pool1.push_back(make_pair(y, x));
    else pool2.push_back(make_pair(y, x));
    
    dfs(mapping, y-1, x, ispool1);
    dfs(mapping, y+1, x, ispool1);
    dfs(mapping, y, x-1, ispool1);
    dfs(mapping, y, x+1, ispool1);
}

int main() {
    cin >> w >> h;
    vector<string> mapping(h);
    rep(i, h) cin >> mapping[i];

    rep(i, h)
        rep(j, w)
            dfs(mapping, i, j, pool1.empty());

    int dist = 20*20;
    for(pair<int, int> p1: pool1) {
        for(pair<int, int> p2: pool2) {
            dist = min(dist, abs(p1.first - p2.first) + abs(p1.second - p2.second) - 1);
        }
    }
    cout << dist << "\n";
}
0