結果

問題 No.2412 YOU Grow Bigger!
ユーザー rniyarniya
提出日時 2023-08-11 23:06:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,737 bytes
コンパイル時間 2,552 ms
コンパイル使用メモリ 218,380 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 14:16:32
合計ジャッジ時間 2,992 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 WA -
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 WA -
testcase_23 AC 4 ms
5,376 KB
testcase_24 WA -
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 5 ms
5,376 KB
testcase_29 WA -
testcase_30 AC 2 ms
5,376 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); }

constexpr int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};

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

    vector ok(H, vector<bool>(W, true));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] != '#') continue;
            ok[i][j] = false;
            for (int k = 0; k < 8; k++) {
                int nx = i + dx[k], ny = j + dy[k];
                if (nx < 0 or H <= nx or ny < 0 or W <= ny) continue;
                ok[nx][ny] = false;
            }
        }
    }
    vector dp(H + 2, vector<int>(W + 2, INF));
    deque<pair<int, int>> que;
    for (int i = 0; i < H; i++) {
        dp[i + 1][0] = 0;
        que.emplace_back(i, -1);
    }
    for (int j = 0; j < W; j++) {
        dp[H + 1][j + 1] = 0;
        que.emplace_back(H, j);
    }
    while (not que.empty()) {
        auto [x, y] = que.front();
        que.pop_front();
        if (x == -1 and y == -1) continue;
        if (x == H and y == W) continue;
        int val = dp[x + 1][y + 1];
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < -1 or H < nx or ny < -1 or W < ny) continue;
            if (nx == -1 or ny == W or (nx < H and -1 < ny and not ok[nx][ny])) {
                if (chmin(dp[nx + 1][ny + 1], val)) que.emplace_front(nx, ny);
            }
        }
        for (int i = -2; i <= 2; i++) {
            for (int j = -2; j <= 2; j++) {
                int cx = x + i, cy = y + j;
                if (cx < 0 or H <= cx or cy < 0 or W <= cy) continue;
                if (cx < 3 and cy < 3) continue;
                if (H - 3 <= cx and W - 3 <= cy) continue;
                bool f = false;
                if (abs(cx - x) + abs(cy - y) <= 1) f = true;
                for (int k = 0; k < 8; k++) {
                    int nx = cx + dx[k], ny = cy + dy[k];
                    if (nx < 0 or H <= nx or ny < 0 or W <= ny) continue;
                    if (abs(nx - x) + abs(ny - y) <= 1) f = true;
                }
                if (not f) continue;
                if (chmin(dp[cx + 1][cy + 1], val + 1)) que.emplace_back(cx, cy);
                for (int k = 0; k < 8; k++) {
                    int nx = cx + dx[k], ny = cy + dy[k];
                    if (nx < 0 or H <= nx or ny < 0 or W <= ny) continue;
                    if (chmin(dp[nx + 1][ny + 1], val + 1)) que.emplace_back(nx, ny);
                }
            }
        }
    }

    int ans = INF;
    for (int i = 0; i < H; i++) chmin(ans, dp[i + 1][W + 1]);
    for (int j = 0; j < W; j++) chmin(ans, dp[0][j + 1]);
    cout << ans << '\n';
    return 0;
}
0