제출 #440562

#제출 시각아이디문제언어결과실행 시간메모리
440562raidAwesome Arrowland Adventure (eJOI19_adventure)C++17
100 / 100
94 ms4988 KiB
#include <iostream> #include <tuple> #include <queue> using namespace std; const int DIM = 505; const int INF = 2e9; char T[DIM][DIM]; priority_queue<tuple<int, int, int>> q; int dist[DIM][DIM]; int dx[] = { 0, -1, 0, 1 }; int dy[] = { -1, 0, 1, 0 }; int costRot( int d1, int d2 ) { // d1 -> d2 int ret = 0; while ( d1 != d2 ) { d1 = (d1 + 1) % 4; ++ret; } return ret; } void dijkstra() { int i, j; q.push({ 0, 1, 1 }); dist[1][1] = 0; while ( !q.empty() ) { i = get<1>(q.top()); j = get<2>(q.top()); q.pop(); if ( T[i][j] != -2 ) { for ( int d = 0; d < 4; ++d ) { int ni = i + dx[d]; int nj = j + dy[d]; if ( T[i][j] != -1 ) { int cost = costRot( T[i][j], d ); if ( dist[i][j] + cost < dist[ni][nj] ) { dist[ni][nj] = dist[i][j] + cost; q.push({ -dist[ni][nj], ni, nj }); } } } } } } int main() { int n, m; cin >> n >> m; for ( int i = 1; i <= n; ++i ) { cin >> (T[i] + 1); for ( int j = 1; j <= m; ++j ) { if ( T[i][j] == 'W' ) { T[i][j] = 0; } else if ( T[i][j] == 'N' ) { T[i][j] = 1; } else if ( T[i][j] == 'E' ) { T[i][j] = 2; } else if ( T[i][j] == 'S' ) { T[i][j] = 3; } else { T[i][j] = -2; } dist[i][j] = INF; } } for ( int i = 0; i <= n + 1; ++i ) { T[i][0] = T[i][m + 1] = -1; } for ( int j = 0; j <= m + 1; ++j ) { T[0][j] = T[n + 1][j] = -1; } dijkstra(); if ( dist[n][m] != INF ) { cout << dist[n][m] << "\n"; } else { cout << "-1\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...