Submission #1274161

#TimeUsernameProblemLanguageResultExecution timeMemory
1274161MisterReaperMaze (JOI23_ho_t3)C++20
100 / 100
469 ms120552 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;
}

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>> dis(R, std::vector<int>(C, R * C));
    std::vector<std::pair<int, int>> que;

    dis[SX][SY] = 0;
    que.emplace_back(SX, SY);
    for (int i = 0; i < int(que.size()); ++i) {
        auto[x, y] = que[i];
        for (int d = 0; d < 4; ++d) {
            int nx = x + dx[d];
            int ny = y + dy[d];
            if (in(nx, ny) && A[nx][ny] == '.' && chmin(dis[nx][ny], 0)) {
                que.emplace_back(nx, ny);
            }
        }
    }

    for (int diss = 1; !que.empty(); ++diss) {
        #ifdef DEBUG
            debug(que);
            std::vector<std::vector<int>> vis(R, std::vector<int>(C));
            for (auto[x, y] : que) {
                vis[x][y] = true;
            }
            for (int i = 0; i < R; ++i) {
                for (int j = 0; j < C; ++j) {
                    std::cerr << vis[i][j] << " \n"[j == C - 1];
                }
            }
        #endif
        std::vector<std::pair<int, int>> guys, nque;
        for (auto[x, y] : que) {
            for (int d = 0; d < 4; ++d) {
                int nx = x + dx[d];
                int ny = y + dy[d];
                if (in(nx, ny) && chmin(dis[nx][ny], diss)) {
                    guys.emplace_back(nx, ny);
                    nque.emplace_back(nx, ny);
                }
            }
        }
        for (int n = 2; n <= N; ++n) {
            std::vector<std::pair<int, int>> nguys;
            for (auto[x, y] : guys) {
                for (int d : {0, 2})  {
                    int nx = x + dx[d];
                    int ny = y + dy[d];
                    if (in(nx, ny) && chmin(dis[nx][ny], diss)) {
                        nguys.emplace_back(nx, ny);
                        nque.emplace_back(nx, ny);
                    }
                }
            }
            guys = std::move(nguys);
        }
        guys = nque;
        for (int n = 2; n <= N; ++n) {
            std::vector<std::pair<int, int>> nguys;
            for (auto[x, y] : guys) {
                for (int d : {1, 3})  {
                    int nx = x + dx[d];
                    int ny = y + dy[d];
                    if (in(nx, ny) && chmin(dis[nx][ny], diss)) {
                        nguys.emplace_back(nx, ny);
                        nque.emplace_back(nx, ny);
                    }
                }
            }
            guys = std::move(nguys);
        }
        que = std::move(nque);
        for (int i = 0; i < int(que.size()); ++i) {
            auto[x, y] = que[i];
            for (int d = 0; d < 4; ++d) {
                int nx = x + dx[d];
                int ny = y + dy[d];
                if (in(nx, ny) && A[nx][ny] == '.' && chmin(dis[nx][ny], diss)) {
                    que.emplace_back(nx, ny);
                }
            }
        }
    }

    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...