제출 #1341547

#제출 시각아이디문제언어결과실행 시간메모리
1341547LucasLeBitaro's Travel 2 (JOI25_travel2)C++20
10 / 100
4094 ms1114112 KiB
#include <bits/stdc++.h>

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

int H, W, L, Q;
int a[3005][3005];
int dist[3005][3005];
bool check[3005][3005];

bool used[3005];
std::vector<int> G[3005];

int encode(int x, int y) {
    return (x - 1) * W + y;
}

int main() {
    // freopen("input.txt", "r", stdin);
    std::cin.tie(0)->sync_with_stdio(0);

    std::cin >> H >> W >> L;
    for (int i = 1; i <= H; ++i) {
        for (int j = 1; j <= W; ++j) {
            std::cin >> a[i][j];
        }
    }

    for (int i = 1; i <= H; ++i) {
        for (int j = 1; j <= W; ++j) {
            std::vector<std::pair<int, int>> vec;
            std::queue<std::pair<int, int>> q;
            q.push({i, j});
            while (!q.empty()) {
                auto [x, y] = q.front();
                vec.push_back({x, y});
                q.pop();
                for (int k = 0; k < 4; ++k) {
                    int nx = x + dx[k];
                    int ny = y + dy[k];
                    if (1 <= nx && nx <= H && 1 <= ny && ny <= W && !check[nx][ny] && a[nx][ny] <= a[i][j] + L) {
                        check[nx][ny] = true;
                        q.push({nx, ny});
                    }
                }
            }
            for (auto [x, y] : vec) {
                G[encode(i, j)].push_back(encode(x, y));
                check[x][y] = false;
            }
        }
    }

    for (int i = 1; i <= H * W; ++i) {
        for (int j = 1; j <= H * W; ++j)
            dist[i][j] = -1;
        dist[i][i] = 0;
    }

    for (int i = 1; i <= H * W; ++i) {
        std::fill(used + 1, used + H * W + 1, false);

        std::queue<int> q;
        used[i] = true;
        q.push(i);

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int v : G[u]) if (!used[v]) {
                used[v] = true;
                dist[i][v] = dist[i][u] + 1;
                q.push(v);
            }
        }
    }

    std::cin >> Q;
    while (Q--) {
        int a, b, x, y;
        std::cin >> a >> b >> x >> y;
        std::cout << dist[encode(a, b)][encode(x, y)] << '\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...