結果

問題 No.157 2つの空洞
ユーザー mmn15277198mmn15277198
提出日時 2021-04-12 23:16:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 3,832 bytes
コンパイル時間 1,659 ms
コンパイル使用メモリ 107,892 KB
実行使用メモリ 6,488 KB
最終ジャッジ日時 2023-09-11 04:03:36
合計ジャッジ時間 2,414 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,220 KB
testcase_01 AC 5 ms
6,196 KB
testcase_02 AC 4 ms
6,288 KB
testcase_03 AC 4 ms
6,204 KB
testcase_04 AC 4 ms
6,444 KB
testcase_05 AC 4 ms
6,212 KB
testcase_06 AC 4 ms
6,212 KB
testcase_07 AC 5 ms
6,452 KB
testcase_08 AC 4 ms
6,352 KB
testcase_09 AC 4 ms
6,384 KB
testcase_10 AC 4 ms
6,448 KB
testcase_11 AC 4 ms
6,488 KB
testcase_12 AC 4 ms
6,204 KB
testcase_13 AC 4 ms
6,356 KB
testcase_14 AC 4 ms
6,480 KB
testcase_15 AC 4 ms
6,412 KB
testcase_16 AC 4 ms
6,288 KB
testcase_17 AC 4 ms
6,284 KB
testcase_18 AC 4 ms
6,204 KB
testcase_19 AC 4 ms
6,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <map>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <queue>

using namespace std;

#define rep(i,n) for (int i = 0; i < (n); i++)
#define vi vector<int>
#define vll vector<long long>
#define vb vector<bool>
#define vs vector<string>
#define all(x) x.begin() , x.end()
#define pb push_back
#define eb emplace_back
#define chmax(x,y) (x) = max((x) , (y))
#define chmin(x,y) (x) = min((x) , (y))
#define fi first
#define se second

typedef long long ll;
typedef long double ld;

//const int MOD = int(1e9) + 7;
//const int MOD = 998244353;
const ll MOD = ll(1e9) + 7;
//const ll MOD = 998244353;
const ld PI = 3.1415926535897932384626;
const int INF = 1001001001;
const ll LINF = 1001001001001001001;
const int MX = 220000;

ll gcd (ll a , ll b) { if (a < b) swap(a , b); if (a % b == 0) return b; else return gcd(b , a % b); }
ll lcm (ll a , ll b) { return a / gcd(a , b) * b; }
ll lin() { ll x; scanf("%lld" , &x); return x; }
int in() { int x; scanf("%d" , &x); return x; }

void io_setup() {
    cout << fixed << setprecision(30);
}

ll modpow (ll x , ll p) {
    ll ret = 1;
    while (p) {
        if (p % 2 == 1) {
            ret *= x;
            ret %= MOD;
        }
        x *= x;
        x %= MOD;
        p /= 2;
    }
    return ret;
}

ll moddiv (ll a , ll b) {
    return a * modpow(b , MOD - 2) % MOD;
}

ll modsub (ll a , ll b) {
    if (a >= b) return (a - b) % MOD;
    else return (MOD - a + b);
}

vll fact(MX);
vll factinv(MX);
void init_fact () {
    fact[0] = 1;
    for (int i = 1; i < MX; i++) {
        fact[i] = 1LL * i * fact[i - 1] % MOD;
    }
    for (int i = 0; i < MX; i++) {
        factinv[i] = modpow(fact[i] , MOD - 2);
    }
}

ll nCk (ll n , ll k) {
    return ((fact[n] * factinv[k] % MOD) * factinv[n - k] % MOD);
}

bool is_prime (int x) {
    if (x == 2 || x == 3 || x == 5 || x == 7) {
        return true;
    }
    for (ll i = 2; i*i <= x; i++) {
        if (x % i == 0) {
            return false;
        }
    }
    return true;
}

vb prime_table(MX , true);
void init_prime() {
    for (int i = 2; i < MX; i++) {
        for (int j = i + i; j < MX; j += i) {
            prime_table[j] = false;
        }
    }
    return;
}

void solve () {
    int W = in();
    int H = in();
    vs S(H);
    rep(i,H) cin >> S[i];
    int id = 0;
    vector<pair<int,int>> a , b;
    rep(i,H)rep(j,W) {
        if (S[i][j] == '.') {
            queue<int> X , Y;
            X.push(j);
            Y.push(i);
            while (!X.empty()) {
                int x = X.front();
                int y = Y.front();
                X.pop();
                Y.pop();
                if (S[y][x] != '.') continue;
                S[y][x] = char('A' + id);
                if (S[y][x] == 'A') a.eb(y , x);
                else if(S[y][x] == 'B') b.eb(y , x);
                if (y - 1 >= 0 && S[y - 1][x] == '.') {
                    X.push(x);
                    Y.push(y - 1);
                }
                if (y + 1 < H && S[y + 1][x] == '.') {
                    X.push(x);
                    Y.push(y + 1);
                }
                if (x - 1 >= 0 && S[y][x - 1] == '.') {
                    X.push(x - 1);
                    Y.push(y);
                }
                if (x + 1 < W && S[y][x + 1] == '.') {
                    X.push(x + 1);
                    Y.push(y);
                }
            }
            id++;
        }
    }
    int ans = INF;
    rep(i,a.size()) {
        rep(j,b.size()) {
            int now = abs(a[i].fi - b[j].fi) + abs(a[i].se - b[j].se) - 1;
            chmin(ans , now);
        }
    }
    cout << ans << endl;
    return;
}

int main() {
    io_setup();
    //init_fact();
    //init_prime();
    solve();
    return 0;
}





0