#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)));
}
}
}
int ans=dijkstra();
if(ans==1e9) printf("-1\n");
else
printf("%d\n", ans);
}
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 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6236 KB |
Output is correct |
10 |
Correct |
2 ms |
6232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6236 KB |
Output is correct |
10 |
Correct |
2 ms |
6232 KB |
Output is correct |
11 |
Correct |
2 ms |
6236 KB |
Output is correct |
12 |
Correct |
2 ms |
6236 KB |
Output is correct |
13 |
Correct |
2 ms |
6236 KB |
Output is correct |
14 |
Correct |
2 ms |
6236 KB |
Output is correct |
15 |
Correct |
2 ms |
6236 KB |
Output is correct |
16 |
Correct |
2 ms |
6172 KB |
Output is correct |
17 |
Correct |
2 ms |
6236 KB |
Output is correct |
18 |
Correct |
2 ms |
6236 KB |
Output is correct |
19 |
Correct |
2 ms |
6236 KB |
Output is correct |
20 |
Correct |
2 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Correct |
2 ms |
6220 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Correct |
2 ms |
6352 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6232 KB |
Output is correct |
10 |
Correct |
2 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6236 KB |
Output is correct |
10 |
Correct |
2 ms |
6232 KB |
Output is correct |
11 |
Correct |
2 ms |
6236 KB |
Output is correct |
12 |
Correct |
2 ms |
6236 KB |
Output is correct |
13 |
Correct |
2 ms |
6236 KB |
Output is correct |
14 |
Correct |
2 ms |
6236 KB |
Output is correct |
15 |
Correct |
2 ms |
6236 KB |
Output is correct |
16 |
Correct |
2 ms |
6172 KB |
Output is correct |
17 |
Correct |
2 ms |
6236 KB |
Output is correct |
18 |
Correct |
2 ms |
6236 KB |
Output is correct |
19 |
Correct |
2 ms |
6236 KB |
Output is correct |
20 |
Correct |
2 ms |
6236 KB |
Output is correct |
21 |
Correct |
2 ms |
6236 KB |
Output is correct |
22 |
Correct |
2 ms |
6220 KB |
Output is correct |
23 |
Correct |
2 ms |
6236 KB |
Output is correct |
24 |
Correct |
2 ms |
6236 KB |
Output is correct |
25 |
Correct |
2 ms |
6236 KB |
Output is correct |
26 |
Correct |
2 ms |
6352 KB |
Output is correct |
27 |
Correct |
2 ms |
6236 KB |
Output is correct |
28 |
Correct |
2 ms |
6236 KB |
Output is correct |
29 |
Correct |
2 ms |
6236 KB |
Output is correct |
30 |
Correct |
2 ms |
6236 KB |
Output is correct |
31 |
Correct |
2 ms |
6236 KB |
Output is correct |
32 |
Correct |
2 ms |
6236 KB |
Output is correct |
33 |
Correct |
2 ms |
6232 KB |
Output is correct |
34 |
Correct |
2 ms |
6236 KB |
Output is correct |
35 |
Correct |
9 ms |
7516 KB |
Output is correct |
36 |
Correct |
2 ms |
6492 KB |
Output is correct |
37 |
Correct |
10 ms |
7768 KB |
Output is correct |
38 |
Correct |
8 ms |
8540 KB |
Output is correct |
39 |
Correct |
85 ms |
21128 KB |
Output is correct |
40 |
Correct |
80 ms |
21068 KB |
Output is correct |
41 |
Correct |
42 ms |
20952 KB |
Output is correct |
42 |
Correct |
80 ms |
21108 KB |
Output is correct |
43 |
Correct |
107 ms |
27304 KB |
Output is correct |
44 |
Correct |
37 ms |
20936 KB |
Output is correct |