#include <bits/stdc++.h>
using namespace std;
int odw[509][509];
string kier[509];
constexpr int oo = 1073741824;
//NESWX
int d[4][4][3] = {{{-1,0,0},{0,1,1},{1,0,2},{0,-1,3}},
{{-1,0,3},{0,1,0},{1,0,1},{0,-1,2}},
{{-1,0,2},{0,1,3},{1,0,0},{0,-1,1}},
{{-1,0,1},{0,1,2},{1,0,3},{0,-1,0}}};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m,x,y,koszt,strona;
string sciana(509,'X');
cin >> n >> m;
kier[0] = sciana;
kier[n+1] = sciana;
for (int i=1;i<=n;i++){
cin >> kier[i];
kier[i] = 'X' + kier[i] + 'X';
}
for( int i =0;i<=n+1;i++){
for (int j=0;j<=m+1;j++){
odw[i][j] = oo;
}
}
priority_queue< pair<int,pair<int,int>> ,vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>> > dijks;
dijks.push({0,{1,1}});
odw[1][1] = 0;
pair<int,pair<int,int>> temp;
while(!dijks.empty()){
temp = dijks.top();
koszt = temp.first;
x = temp.second.first;
y = temp.second.second;
dijks.pop();
if (kier[x][y] == 'N'){
strona = 0;
}
if (kier[x][y] == 'E'){
strona = 1;
}
if (kier[x][y] == 'S'){
strona = 2;
}
if (kier[x][y] == 'W'){
strona = 3;
}
if (kier[x][y] == 'X'){
continue;
}
for (int i=0;i<4;i++){
if (odw[x + d[strona][i][0]][y + d[strona][i][1]] > odw[x][y] + d[strona][i][2]){
odw[x + d[strona][i][0]][y + d[strona][i][1]] = odw[x][y] + d[strona][i][2];
dijks.push({odw[x][y] + d[strona][i][2],{x + d[strona][i][0],y + d[strona][i][1]}});
}
}
}
if (odw[n][m] == oo){
cout << "-1\n";
}
else{
cout << odw[n][m] << "\n";
}
return 0;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:17:17: warning: variable 'koszt' set but not used [-Wunused-but-set-variable]
17 | int n,m,x,y,koszt,strona;
| ^~~~~
adventure.cpp:17:23: warning: 'strona' may be used uninitialized in this function [-Wmaybe-uninitialized]
17 | int n,m,x,y,koszt,strona;
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
4 ms |
484 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
5 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
920 KB |
Output is correct |
39 |
Correct |
41 ms |
2116 KB |
Output is correct |
40 |
Correct |
42 ms |
2128 KB |
Output is correct |
41 |
Correct |
1 ms |
2004 KB |
Output is correct |
42 |
Correct |
41 ms |
2128 KB |
Output is correct |
43 |
Correct |
69 ms |
5324 KB |
Output is correct |
44 |
Correct |
2 ms |
2004 KB |
Output is correct |