Submission #563399

# Submission time Handle Problem Language Result Execution time Memory
563399 2022-05-17T06:27:59 Z KoD Robots (APIO13_robots) C++17
30 / 100
99 ms 75448 KB
#include <bits/stdc++.h>

using std::pair;
using std::tuple;
using std::vector;
using std::array;

constexpr int MAX = 500;
constexpr int INF = std::numeric_limits<int>::max() / 2;
constexpr int DI[] = {0, 1, 0, -1};
constexpr int DJ[] = {1, 0, -1, 0};

char grid[MAX + 2][MAX + 2];
int dist[MAX + 2][MAX + 2][9][9];
int state[MAX + 2][MAX + 2][4];
pair<int, int> stops[MAX + 2][MAX + 2][4];

pair<int, int> dfs(const int i, const int j, const int k) {
    if (state[i][j][k] == 2) {
        return stops[i][j][k];
    }
    if (state[i][j][k] == 1) {
        return stops[i][j][k] = {i, j};
    }
    state[i][j][k] = 1;
    int nk = k;
    if (grid[i][j] == 'A') {
        nk = (k + 3) % 4;
    }
    if (grid[i][j] == 'C') {
        nk = (k + 1) % 4;
    }
    const int ni = i + DI[nk], nj = j + DJ[nk];
    if (grid[ni][nj] == 'x') {
        state[i][j][k] = 2;
        return stops[i][j][k] = {i, j};
    } else {
        stops[i][j][k] = dfs(ni, nj, nk);
        if (state[ni][nj][nk] == 1) {
            stops[i][j][k] = {i, j};
        } else {
            state[i][j][k] = 2;
        }
        return stops[i][j][k];
    }
}

template <class T> struct FastQueue {
    vector<T> vec;
    int id, size;
    FastQueue(const int n) : vec(n), id(0), size(0) {}
    void push(const T& x) {
        assert(size < (int)vec.size());
        vec[size++] = x;
    }
    T pop() {
        return vec[id++];
    }
    bool empty() const {
        return id == size;
    }
    void clear() {
        id = size = 0;
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, H, W;
    std::cin >> N >> W >> H;
    H += 2, W += 2;
    for (int i = 0; i < W; ++i) {
        grid[0][i] = grid[H - 1][i] = 'x';
    }
    for (int i = 0; i < H; ++i) {
        grid[i][0] = grid[i][W - 1] = 'x';
    }
    for (int i = 1; i < H - 1; ++i) {
        for (int j = 1; j < W - 1; ++j) {
            std::cin >> grid[i][j];
        }
    }
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            for (int k = 0; k < N; ++k) {
                for (int l = 0; l < N; ++l) {
                    dist[i][j][k][l] = INF;
                }
            }
        }
    }
    //*
    FastQueue<pair<int, int>> que(MAX * MAX);
    for (int len = 0; len < N; ++len) {
        for (int l = 0; l + len < N; ++l) {
            const int r = l + len;
            const auto push = [&](const int i, const int j, const int d) {
                if (dist[i][j][l][r] > d) {
                    dist[i][j][l][r] = d;
                    que.push({i, j});
                }
            };
            que.clear();
            if (len == 0) {
                for (int i = 0; i < H; ++i) {
                    for (int j = 0; j < W; ++j) {
                        if (grid[i][j] == '1' + l) {
                            push(i, j, 0);
                        }
                    }
                }
            } else {
                for (int i = 0; i < H; ++i) {
                    for (int j = 0; j < W; ++j) {
                        for (int k = l; k < r; ++k) {
                            push(i, j, dist[i][j][l][k] + dist[i][j][k + 1][r]);
                        }
                    }
                }
            }
            while (!que.empty()) {
                const auto [i, j] = que.pop();
                for (int d = 0; d < 4; ++d) {
                    const auto [ni, nj] = dfs(i, j, d);
                    push(ni, nj, dist[i][j][l][r] + 1);
                }
            }
        }
    }
    //*/
    /*
    std::queue<array<int, 4>> que;
    const auto push = [&](const int i, const int j, const int k, const int l, const int d) {
        if (dist[i][j][k][l] > d) {
            dist[i][j][k][l] = d;
            que.push({i, j, k, l});
        }
    };
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            if ('1' <= grid[i][j] and grid[i][j] <= '9') {
                const int c = grid[i][j] - '1';
                push(i, j, c, c, 0);
            }
        }
    }
    while (!que.empty()) {
        const auto [i, j, k, l] = que.front();
        que.pop();
        if (k > 0) {
            push(i, j, k - 1, l, dist[i][j][k][l] + dist[i][j][k - 1][k - 1]);
        }
        if (l + 1 < N) {
            push(i, j, k, l + 1, dist[i][j][k][l] + dist[i][j][l + 1][l + 1]);
        }
        for (int d = 0; d < 4; ++d) {
            const auto [ni, nj] = dfs(i, j, d);
            push(ni, nj, k, l, dist[i][j][k][l] + 1);
        }
    }
    //*/
    int ans = INF;
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            ans = std::min(ans, dist[i][j][0][N - 1]);
        }
    }
    std::cout << (ans == INF ? -1 : ans) << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2260 KB Output is correct
2 Correct 2 ms 2260 KB Output is correct
3 Correct 1 ms 2260 KB Output is correct
4 Correct 2 ms 2388 KB Output is correct
5 Correct 2 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2260 KB Output is correct
2 Correct 2 ms 2260 KB Output is correct
3 Correct 1 ms 2260 KB Output is correct
4 Correct 2 ms 2388 KB Output is correct
5 Correct 2 ms 2388 KB Output is correct
6 Correct 1 ms 2260 KB Output is correct
7 Correct 1 ms 2260 KB Output is correct
8 Correct 2 ms 2260 KB Output is correct
9 Correct 2 ms 2260 KB Output is correct
10 Correct 2 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2260 KB Output is correct
2 Correct 2 ms 2260 KB Output is correct
3 Correct 1 ms 2260 KB Output is correct
4 Correct 2 ms 2388 KB Output is correct
5 Correct 2 ms 2388 KB Output is correct
6 Correct 1 ms 2260 KB Output is correct
7 Correct 1 ms 2260 KB Output is correct
8 Correct 2 ms 2260 KB Output is correct
9 Correct 2 ms 2260 KB Output is correct
10 Correct 2 ms 2388 KB Output is correct
11 Runtime error 99 ms 75448 KB Execution killed with signal 6
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2260 KB Output is correct
2 Correct 2 ms 2260 KB Output is correct
3 Correct 1 ms 2260 KB Output is correct
4 Correct 2 ms 2388 KB Output is correct
5 Correct 2 ms 2388 KB Output is correct
6 Correct 1 ms 2260 KB Output is correct
7 Correct 1 ms 2260 KB Output is correct
8 Correct 2 ms 2260 KB Output is correct
9 Correct 2 ms 2260 KB Output is correct
10 Correct 2 ms 2388 KB Output is correct
11 Runtime error 99 ms 75448 KB Execution killed with signal 6
12 Halted 0 ms 0 KB -