#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
int n, m, val;
string str;
pair<int,int> y;
pair<int, pair<int,int>> x;
priority_queue<pair<int, pair<int,int>>> pq;
vector<pair<int,int>> dir = {
{0, 1},
{1, 0},
{0, -1},
{-1, 0}
};
int main() {
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m)), path(n, vector<int>(m, INT_MAX));
for(int i = 0; i < n; i++) {
cin >> str;
for(int j = 0; j < m; j++) {
switch(str[j]) {
case 'N':
a[i][j] = 3;
break;
case 'E':
a[i][j] = 0;
break;
case 'S':
a[i][j] = 1;
break;
case 'W':
a[i][j] = 2;
break;
default:
a[i][j] = -1;
break;
}
}
}
pq.push({0, {0, 0}});
while(pq.size()) {
x = pq.top();
pq.pop();
x.first = -x.first;
if(x.first >= path[x.second.first][x.second.second]) continue;
//cout << x.first << " " << x.second.first << " " << x.second.second << "\n";
path[x.second.first][x.second.second] = x.first;
for(int i = 0; i < dir.size(); i++) {
y = x.second;
y.first+=dir[i].first;
y.second+=dir[i].second;
if(y.first < 0 || y.first > n-1 || y.second < 0 || y.second > m-1 || (a[y.first][y.second] == -1 && (y.first != n-1 || y.second != m-1))) continue;
val = (4-a[x.second.first][x.second.second]+i)%4;
//cout << "VAL " << val << " " << a[x.second.first][x.second.second] << " " << i << "\n";
if(path[x.second.first][x.second.second]+val < path[y.first][y.second]) pq.push({-(path[x.second.first][x.second.second]+val), y});
}
}
cout << path[n-1][m-1] << "\n";
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:53:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 0; i < dir.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |