제출 #1274123

#제출 시각아이디문제언어결과실행 시간메모리
1274123MisterReaperMaze (JOI23_ho_t3)C++20
35 / 100
2095 ms39212 KiB
// File maze.cpp created on 29.09.2025 at 10:19:41
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG 
    #include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
    #define debug(...) void(23)
#endif

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

template<typename T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

struct fenwick {
    int n, m;
    std::vector<std::vector<int>> tree;
    fenwick(int n_, int m_) : n(n_), m(m_), tree(n + 1, std::vector<int>(m + 1)) {}
    void modify(int px, int py, int x) {
        for (int a = px + 1; a <= n; a += a & -a) {
            for (int b = py + 1; b <= m; b += b & -b) {
                tree[a][b] += x;
            }
        }
    }
    void modify(int a, int b, int c, int d, int x) {
        a = std::max(a, 0);
        b = std::max(b, 0);
        c = std::min(c, n - 1);
        d = std::min(d, m - 1);
        debug(a, b, c, d);
        modify(a, b, +x);
        modify(a, d + 1, -x);
        modify(c + 1, b, -x);
        modify(c + 1, d + 1, +x);
    }
    int get(int px, int py) {
        int res = 0;
        for (int a = px + 1; a; a -= a & -a) {
            for (int b = py + 1; b; b -= b & -b) {
                res += tree[a][b];
            }
        }
        return res;
    }
};

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

    int R, C, N;
    std::cin >> R >> C >> N;

    auto in = [&](int x, int y) -> bool {
        return 0 <= x && x < R && 0 <= y && y < C;
    };

    int SX, SY, GX, GY;
    std::cin >> SX >> SY >> GX >> GY;
    --SX, --SY, --GX, --GY;

    std::vector<std::string> A(R);
    for (int i = 0; i < R; ++i) {
        std::cin >> A[i];
    }

    std::vector<std::vector<int>> vis(R, std::vector<int>(C));
    std::vector<std::vector<int>> dis(R, std::vector<int>(C, R * C));
    std::vector<std::pair<int, int>> deq;

    dis[SX][SY] = 0;
    deq.emplace_back(SX, SY);

    for (int diss = 0; ; ++diss) {
        fenwick fen(R, C);
        std::vector<std::pair<int, int>> ndeq;
        for (int i = 0; i < int(deq.size()); ++i) {
            auto[x, y] = deq[i];
            if (dis[x][y] != diss) {
                continue;
            }
            debug(x, y, diss);
            for (int d = 0; d < 4; ++d) {
                int nx = x + dx[d];
                int ny = y + dy[d];
                if (in(nx, ny)) {
                    if (A[nx][ny] == '.') {
                        if (chmin(dis[nx][ny], dis[x][y])) {
                            deq.emplace_back(nx, ny);
                        }
                    } 
                }
            }
            fen.modify(x - N, y - N, x + N, y + N, +1);
            if (in(x - N, y - N)) {
                fen.modify(x - N, y - N, x - N, y - N, -1);
            }
            if (in(x - N, y + N)) {
                fen.modify(x - N, y + N, x - N, y + N, -1);
            }
            if (in(x + N, y - N)) {
                fen.modify(x + N, y - N, x + N, y - N, -1);
            }
            if (in(x + N, y + N)) {
                fen.modify(x + N, y + N, x + N, y + N, -1);
            }
            // for (int DX = -N; DX <= N; ++DX) {
            //     for (int DY = -N; DY <= N; ++DY) {
            //         if (std::abs(DX) == N && std::abs(DY) == N) {
            //             continue;
            //         }

            //         int X = x + DX;
            //         int Y = y + DY;
            //         if (0 <= X && X < R && 0 <= Y && Y < C) {
            //             if (chmin(dis[X][Y], dis[x][y] + 1)) {
            //                 ndeq.emplace_back(X, Y);
            //             }
            //         }
            //     }
            // }
        }
        #ifdef DEBUG
            for (int i = 0; i < R; ++i) {
                for (int j = 0; j < C; ++j) {
                    std::cerr << fen.get(i, j) << " \n"[j == C - 1];
                }
            }
        #endif
        for (int i = 0; i < R; ++i) {
            for (int j = 0; j < C; ++j) {
                if (fen.get(i, j) > 0 && dis[i][j] > diss + 1) {
                    dis[i][j] = diss + 1;
                    ndeq.emplace_back(i, j);
                }
            }
        }
        if (ndeq.empty()) {
            break;
        }
        deq = std::move(ndeq);
    }

    std::cout << dis[GX][GY] << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...