답안 #742546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
742546 2023-05-16T12:48:20 Z bogdanvladmihai Awesome Arrowland Adventure (eJOI19_adventure) C++14
22 / 100
1 ms 212 KB
#include <iostream>
#include <vector>
#include <set>
#include <utility>
#include <limits.h>

const std::vector<std::pair<int, int>> dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};

struct Node {
    int cost;
    int x, y;
    Node(int cost_, int x_, int y_) : cost(cost_), x(x_), y(y_) {}
    bool operator < (const Node &other) const {
        return cost < other.cost;
    }
};

inline bool inside(int x, int y, int N, int M) {
    return x >= 0 && y >= 0 && x < N && y < M;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    int N, M;
    std::cin >> N >> M;
    std::vector<std::string> mat(N);
    for (auto &line : mat) {
        std::cin >> line;
    }
    std::set<Node> Q;
    std::vector<std::vector<int>> dist(N, std::vector<int>(M, INT_MAX / 2));
    if (mat[0][0] != 'X') {
        dist[0][0] = 0;
        Q.insert(Node(0, 0, 0));
    }
    while (!Q.empty()) {
        int cost = Q.begin() -> cost, x = Q.begin() -> x, y = Q.begin() -> y;
        if (x == N - 1 && y == M - 1) {
            break;
        }
        Q.erase(Q.begin());
        if (cost > dist[x][y]) {
            continue;
        }
        int j = 0;
        switch (mat[x][y]) {
            case 'N':
                j = 1;
                break;
            case 'E':
                j = 2;
                break;
            case 'S':
                j = 3;
                break;
        }
        for (int i = 0; i < 4; i++) {
            int nx = x + dirs[(i + j) % 4].first, ny = y + dirs[(i + j) % 4].second;
            if (inside(nx, ny, N, M) && dist[nx][ny] > dist[x][y] + i) {
                int old = dist[nx][ny];
                dist[nx][ny] = dist[x][y] + i;
                if (mat[nx][ny] != 'X' || (nx == N - 1 && ny == M - 1)) {
                    if (Q.find(Node(old, nx, ny)) != Q.end()) {
                        Q.erase(Node(old, nx, ny));
                    }
                    Q.insert(Node(dist[nx][ny], nx, ny));
                }
            }
        }
    }
    std::cout << (dist[N - 1][M - 1] < INT_MAX / 2 ? dist[N - 1][M - 1] : -1) << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Incorrect 0 ms 212 KB Output isn't correct
22 Halted 0 ms 0 KB -