結果

問題 No.2412 YOU Grow Bigger!
ユーザー rniyarniya
提出日時 2023-08-17 01:42:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 6,000 ms
コード長 2,991 bytes
コンパイル時間 2,005 ms
コンパイル使用メモリ 214,336 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-04 08:55:37
合計ジャッジ時間 3,912 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 165 ms
6,944 KB
testcase_04 AC 166 ms
6,944 KB
testcase_05 AC 127 ms
6,940 KB
testcase_06 AC 147 ms
6,944 KB
testcase_07 AC 166 ms
6,940 KB
testcase_08 AC 149 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 6 ms
6,940 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 20 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 6 ms
6,940 KB
testcase_23 AC 35 ms
6,944 KB
testcase_24 AC 67 ms
6,944 KB
testcase_25 AC 33 ms
6,944 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 10 ms
6,940 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    for (auto& s : S) cin >> s;

    vector ok(H, vector<bool>(W, false)), dp(H, vector<bool>(W, false));
    auto check = [&]() {
        for (int x = 1; x < H - 1; x++) {
            for (int y = 1; y < W - 1; y++) {
                ok[x][y] = true;
                dp[x][y] = false;
            }
        }
        for (int x = 0; x < H; x++) {
            for (int y = 0; y < W; y++) {
                if (S[x][y] != '#') continue;
                for (int dx = -1; dx <= 1; dx++) {
                    for (int dy = -1; dy <= 1; dy++) {
                        int nx = x + dx, ny = y + dy;
                        if (nx < 0 or H <= nx or ny < 0 or W <= ny) continue;
                        ok[nx][ny] = false;
                    }
                }
            }
        }
        queue<pair<int, int>> que;
        dp[1][1] = true;
        que.emplace(1, 1);
        while (not que.empty()) {
            auto [x, y] = que.front();
            que.pop();
            for (int dx = -1; dx <= 1; dx++) {
                for (int dy = -1; dy <= 1; dy++) {
                    int nx = x + dx, ny = y + dy;
                    if (not ok[nx][ny]) continue;
                    if (dp[nx][ny]) continue;
                    dp[nx][ny] = true;
                    que.emplace(nx, ny);
                }
            }
        }
        return dp[H - 2][W - 2];
    };

    if (not check()) {
        cout << 0 << '\n';
        return 0;
    }

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (i < 3 and j < 3) continue;
            if (H - 3 <= i and W - 3 <= j) continue;
            if (S[i][j] == '#') continue;
            S[i][j] = '#';
            if (not check()) {
                cout << 1 << '\n';
                return 0;
            }
            S[i][j] = '.';
        }
    }

    cout << 2 << '\n';
    return 0;
}
0