답안 #968560

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
968560 2024-04-23T15:40:51 Z vjudge1 Awesome Arrowland Adventure (eJOI19_adventure) C++17
0 / 100
1 ms 352 KB
#include<bits/stdc++.h>
using namespace std;

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

bool between(int x, int l, int r) {
    return l <= x && x <= r;
}

const int MAXN = 500;
const int INF = 1e9;
typedef tuple<int, int, int, int> state;

int N, M;
string GRID[MAXN + 5];
int dist[MAXN + 5][MAXN + 5][5];
priority_queue<state, vector<state>, greater<state> > PQ;

inline bool inside(int x, int y) {
    return between(x, 0, N - 1) && between(y, 0, M - 1);
}

inline int getDir(char c) {
    if (c == 'N') return 0;
    if (c == 'E') return 1;
    if (c == 'S') return 2;
    if (c == 'W') return 3;
    return 4;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> N >> M;
    for (int i = 0; i < N; i++) cin >> GRID[i];

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            for (int k = 0; k < 5; k++) {
                dist[i][j][k] = INF;
            }
        }
    }

    dist[0][0][getDir(GRID[0][0])] = 0;
    PQ.push({0, 0, 0, getDir(GRID[0][0])});
    while (!PQ.empty()) {
        auto [cost, x, y, d] = PQ.top();
        PQ.pop();

        if (d == 4) continue;
        
        int nx = x + dx[d], ny = y + dy[d];
        if (inside(nx, ny)) {
            int nd = getDir(GRID[nx][ny]);
            if (dist[nx][ny][nd] > cost) {
                dist[nx][ny][nd] = cost;
                PQ.push({cost, nx, ny, nd});
            }
        }
        
        int nd = (d + 1) % 4;
        if (dist[x][y][nd] > cost + 1) {
            dist[x][y][nd] = cost + 1;
            PQ.push({cost + 1, x, y, nd});
        }
    }

    cout << dist[N - 1][M - 1][4] << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 352 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 352 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Incorrect 1 ms 352 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 352 KB Output isn't correct
2 Halted 0 ms 0 KB -