답안 #563402

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563402 2022-05-17T06:30:24 Z KoD 로봇 (APIO13_robots) C++17
30 / 100
93 ms 75496 KB
#include <bits/stdc++.h>

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

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) { 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) {
                        int min = INF;
                        for (int k = l; k < r; ++k) {
                            min = std::min(min, dist[i][j][l][k] + dist[i][j][k + 1][r]);
                        }
                        push(i, j, min);
                    }
                }
            }
            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);
                }
            }
        }
    }
    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Correct 1 ms 2260 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 2 ms 2388 KB Output is correct
5 Correct 1 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Correct 1 ms 2260 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 2 ms 2388 KB Output is correct
5 Correct 1 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 1 ms 2260 KB Output is correct
9 Correct 2 ms 2260 KB Output is correct
10 Correct 1 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Correct 1 ms 2260 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 2 ms 2388 KB Output is correct
5 Correct 1 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 1 ms 2260 KB Output is correct
9 Correct 2 ms 2260 KB Output is correct
10 Correct 1 ms 2388 KB Output is correct
11 Runtime error 93 ms 75496 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Correct 1 ms 2260 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 2 ms 2388 KB Output is correct
5 Correct 1 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 1 ms 2260 KB Output is correct
9 Correct 2 ms 2260 KB Output is correct
10 Correct 1 ms 2388 KB Output is correct
11 Runtime error 93 ms 75496 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -