結果

問題 No.157 2つの空洞
ユーザー koyoprokoyopro
提出日時 2015-09-21 19:37:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 3,383 bytes
コンパイル時間 830 ms
コンパイル使用メモリ 79,964 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-26 14:11:30
合計ジャッジ時間 1,876 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<math.h>
#include<vector>
#include <array>
#include <algorithm>
#include <queue>
#include <numeric>
#include <map>

using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) //printf(__VA_ARGS__)

typedef pair<int, int> pos;
int pos::*x = &pos::first;
int pos::*y = &pos::second;

int dxy[] = {0, 1, 0, -1, 0};

/*================================*/

#define MAX 20
int W, H;
pos start;

static bool isCombine(bool map[MAX][MAX]) {
    // マップ内の空洞数
    int space = 0;
    REP(h,H)REP(w,W) {
        if (map[w][h]) space++;
    }
    // スタートから繋がってる場所カウント
    queue<pos> que;
    que.push(start);
    vector<pos> checked;
    checked.push_back(start);
    int space2 = 1;
    while (que.size()) {
        pos p = que.front();
        que.pop();
        REP(i,4) {
            pos np;
            np.*x = p.first + dxy[i];
            np.*y = p.second + dxy[i+1];
            if (np.*x < 0 || W <= np.*x) continue;
            if (np.*y < 0 || H <= np.*y) continue;
            if (CONTAIN(checked, np)) continue;
            if (map[np.*x][np.*y]) {
                checked.push_back(np);
                que.push(np);
                space2++;
            }
        }
    }
    //cout << "space: " << space << "," << space2 << endl;
    return (space == space2);
}

void expand(bool map[MAX][MAX]) {
    // スタートから繋がってる場所
    queue<pos> que;
    que.push(start);
    vector<pos> checked;
    checked.push_back(start);
    while (que.size()) {
        pos p = que.front();
        que.pop();
        REP(i,4) {
            pos np;
            np.*x = p.first + dxy[i];
            np.*y = p.second + dxy[i+1];
            if (np.*x < 0 || W <= np.*x) continue;
            if (np.*y < 0 || H <= np.*y) continue;
            if (CONTAIN(checked, np)) continue;
            if (map[np.*x][np.*y]) {
                checked.push_back(np);
                que.push(np);
            }
        }
    }
    // 空洞の一つ隣を全部空ける
    REP(j,checked.size()) {
        pos p = checked[j];
        REP(i,4) {
            pos np;
            np.*x = p.first + dxy[i];
            np.*y = p.second + dxy[i+1];
            if (np.*x < 0 || W <= np.*x) continue;
            if (np.*y < 0 || H <= np.*y) continue;
            if (CONTAIN(checked, np)) continue;
            map[np.*x][np.*y] = true; // 無条件で空洞に
        }
    }
}

signed main()
{
    cin >> W >> H;
    bool map[MAX][MAX];
    REP(h,H) {
        REP(w,W) {
            char c;
            cin >> c;
            // 空洞はtrue
            map[w][h] = (c == '.');
            if (start.*x == 0 && map[w][h]) {
                start.*x = w;
                start.*y = h;
            }
        }
    }
    int ccnt = 0;
    while (true) {
        if (isCombine(map)) break;
        expand(map);
        ccnt++;
    }
    cout << ccnt << endl;

    return 0;
}
0