#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int,int> pi;
vector<pair<int, pi>> g[502][502];
int N, M;
int dijkstra(){
int d[M][N], z[M][N];
for(int i=0; i < M; i++) for(int j=0; j < N; j++){
d[i][j] = 1e9;
z[i][j] = 0;
}
d[0][0] = 0;
priority_queue<pair<int, pi>> pq;
pq.push(make_pair(0, make_pair(0, 0)));
while(!pq.empty()){
int i=pq.top().second.first;
int j=pq.top().second.second;
pq.pop();
if(z[i][j]) continue;
z[i][j] = 1;
for(auto u: g[i][j]){
if(d[u.second.first][u.second.second] > d[i][j] + u.first){
d[u.second.first][u.second.second] = d[i][j] + u.first;
pq.push(make_pair(-d[u.second.first][u.second.second], make_pair(u.second.first, u.second.second)));
}
}
}
return d[M-1][N-1];
}
int main()
{
scanf("%d%d", &M, &N);
for(int i=0; i < M; i++){
for(int j=0; j < N; j++){
char o;
scanf(" %c", &o);
if(o=='E'){
if(j < N-1) g[i][j].push_back(make_pair(0, make_pair(i, j+1)));
if(i < M-1) g[i][j].push_back(make_pair(1, make_pair(i+1, j)));
if(j) g[i][j].push_back(make_pair(2, make_pair(i, j-1)));
if(i) g[i][j].push_back(make_pair(3, make_pair(i-1, j)));
}
else if(o=='N'){
if(j < N-1) g[i][j].push_back(make_pair(1, make_pair(i, j+1)));
if(i < M-1) g[i][j].push_back(make_pair(2, make_pair(i+1, j)));
if(j) g[i][j].push_back(make_pair(3, make_pair(i, j-1)));
if(i) g[i][j].push_back(make_pair(0, make_pair(i-1, j)));
}
else if(o=='W'){
if(j < N-1) g[i][j].push_back(make_pair(2, make_pair(i, j+1)));
if(i < M-1) g[i][j].push_back(make_pair(3, make_pair(i+1, j)));
if(j) g[i][j].push_back(make_pair(0, make_pair(i, j-1)));
if(i) g[i][j].push_back(make_pair(1, make_pair(i-1, j)));
}
else if(o=='S'){
if(j < N-1) g[i][j].push_back(make_pair(3, make_pair(i, j+1)));
if(i < M-1) g[i][j].push_back(make_pair(0, make_pair(i+1, j)));
if(j) g[i][j].push_back(make_pair(1, make_pair(i, j-1)));
if(i) g[i][j].push_back(make_pair(2, make_pair(i-1, j)));
}
}
}
printf("%d\n", dijkstra());
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d%d", &M, &N);
| ~~~~~^~~~~~~~~~~~~~~~
adventure.cpp:45:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf(" %c", &o);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Incorrect |
2 ms |
6236 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |