結果

問題 No.2412 YOU Grow Bigger!
ユーザー hari64hari64
提出日時 2023-07-19 22:38:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,162 ms / 6,000 ms
コード長 3,828 bytes
コンパイル時間 2,657 ms
コンパイル使用メモリ 228,892 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 06:33:24
合計ジャッジ時間 11,239 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1,156 ms
6,940 KB
testcase_04 AC 1,161 ms
6,940 KB
testcase_05 AC 745 ms
6,940 KB
testcase_06 AC 983 ms
6,940 KB
testcase_07 AC 1,162 ms
6,944 KB
testcase_08 AC 936 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 20 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 36 ms
6,940 KB
testcase_14 AC 4 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 119 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 19 ms
6,944 KB
testcase_22 AC 28 ms
6,940 KB
testcase_23 AC 222 ms
6,940 KB
testcase_24 AC 465 ms
6,940 KB
testcase_25 AC 206 ms
6,944 KB
testcase_26 AC 14 ms
6,940 KB
testcase_27 AC 6 ms
6,944 KB
testcase_28 AC 55 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef hari64
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define debug(...)
#else
#include "util/viewer.hpp"
#define debug(...) viewer::_debug(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#endif
// clang-format off
using namespace std;
using ll = long long; using ld = long double;
using pii = pair<int, int>; using pll = pair<ll, ll>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vc<T>>;
template <typename T> using vvvc = vector<vvc<T>>;
using vi = vc<int>; using vll = vc<ll>; using vpii = vc<pii>; using vpll = vc<pll>;
using vvi = vc<vi>; using vvll = vc<vll>; using vvpi = vc<vpii>; using vvpll = vc<vpll>;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
constexpr int INF = 1001001001; constexpr long long INFll = 1001001001001001001;
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
// clang-format on

int H, W;
vector<string> Ss;

bool canReach() {
    vector<vector<bool>> grid(H - 2, vector<bool>(W - 2, INF));
    for (int h = 1; h < H - 1; h++) {
        for (int w = 1; w < W - 1; w++) {
            grid[h - 1][w - 1] = true;
            for (int dh = -1; dh <= 1; dh++) {
                for (int dw = -1; dw <= 1; dw++) {
                    if (Ss[h + dh][w + dw] == '#') {
                        grid[h - 1][w - 1] = false;
                    }
                }
            }
        }
    }
    assert(grid[0][0] == true && grid[H - 3][W - 3] == true);

    // 0,0からH-3,W-3までの経路があるか
    vvc<vpii> adj(H - 2, vc<vpii>(W - 2));
    for (int h = 0; h < H - 2; h++) {
        for (int w = 0; w < W - 2; w++) {
            if (!grid[h][w]) continue;
            for (int dh = -1; dh <= 1; dh++) {
                for (int dw = -1; dw <= 1; dw++) {
                    if (dh == 0 && dw == 0) continue;
                    if (h + dh < 0 || h + dh >= H - 2) continue;
                    if (w + dw < 0 || w + dw >= W - 2) continue;
                    if (grid[h + dh][w + dw]) {
                        adj[h][w].push_back({h + dh, w + dw});
                    }
                }
            }
        }
    }

    queue<pii> q;
    q.push({0, 0});
    vector<vector<bool>> visited(H - 2, vector<bool>(W - 2));
    visited[0][0] = true;
    while (!q.empty()) {
        auto [h, w] = q.front();
        q.pop();
        for (auto [nh, nw] : adj[h][w]) {
            if (visited[nh][nw]) continue;
            visited[nh][nw] = true;
            q.push({nh, nw});
        }
    }

    return visited[H - 3][W - 3];
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> H >> W;
    assert(6 <= H && 6 <= W);
    assert(H * W <= 2400);
    Ss.resize(H);
    for (auto& S : Ss) {
        cin >> S;
        assert(int(S.size()) == W);
        for (auto& c : S) {
            assert(c == '.' || c == '#');
        }
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            assert(Ss[i][j] == '.');
        }
    }
    for (int i = H - 3; i < H; i++) {
        for (int j = W - 3; j < W; j++) {
            assert(Ss[i][j] == '.');
        }
    }

    int ans = 2;
    if (!canReach()) {
        ans = 0;
    } else {
        for (int h = 0; h < H && ans == 2; h++) {
            for (int w = 0; w < W && ans == 2; w++) {
                if (h < 3 && w < 3) continue;
                if (h >= H - 3 && w >= W - 3) continue;
                if (Ss[h][w] == '#') continue;
                Ss[h][w] = '#';
                if (!canReach()) {
                    ans = 1;
                }
                Ss[h][w] = '.';
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0