Submission #1274125

#TimeUsernameProblemLanguageResultExecution timeMemory
1274125MisterReaperMaze (JOI23_ho_t3)C++20
35 / 100
2093 ms39552 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 prefixsum { int n, m; std::vector<std::vector<int>> mat; prefixsum(int n_, int m_) : n(n_), m(m_), mat(n + 1, std::vector<int>(m + 1)) {} 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); mat[a][b] += x; mat[a][d + 1] -= x; mat[c + 1][b] -= x; mat[c + 1][d + 1] += x; } void build() { for (int i = 0; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (i) { mat[i][j] += mat[i - 1][j]; } if (j) { mat[i][j] += mat[i][j - 1]; } if (i && j) { mat[i][j] -= mat[i - 1][j - 1]; } } } } }; 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) { prefixsum ps(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); } } } } ps.modify(x - N, y - N, x + N, y + N, +1); if (in(x - N, y - N)) { ps.modify(x - N, y - N, x - N, y - N, -1); } if (in(x - N, y + N)) { ps.modify(x - N, y + N, x - N, y + N, -1); } if (in(x + N, y - N)) { ps.modify(x + N, y - N, x + N, y - N, -1); } if (in(x + N, y + N)) { ps.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); // } // } // } // } } ps.build(); for (int i = 0; i < R; ++i) { for (int j = 0; j < C; ++j) { if (ps.mat[i][j] && 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...