#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define pb push_back
#define pf push_front
#define vi vector<int>
#define pi pair<int,int>
#define pii pair<ll,pi>
const int MAX = 510;
int g[MAX][MAX];
const int mod = 4;
int dy[4]={1,0,-1,0};
int dx[4] ={0, 1,0,-1};
int dir[4] = {1,2,3,4};
int lmatch(char c){
if(c=='E') return 1;
else if(c=='S') return 2;
else if(c=='W') return 3;
else if(c=='N')return 4;
return 5;
}
int vis[MAX][MAX];
ll dis[MAX][MAX];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,m;
cin >> n >> m;
char c;
for(int i = 1; i<= n; i++){
for(int j = 1; j<=m; j++){
cin >> c;
g[i][j] =lmatch(c);
}
}
priority_queue<pii, vector<pii>, greater<pii>>pq;
pq.push({0,{1,1}});
ll ncost =0;
int ans = INT_MAX;
for(int i =0; i <= n; i++){
for(int j = 0; j <= m; j++){
dis[i][j]=INT_MAX;
}
}
dis[1][1] = 0;
while(!pq.empty()){
int curx = pq.top().s.f;
int cury = pq.top().s.s;
int cost = pq.top().f;
pq.pop();
if(vis[curx][cury])continue;
vis[curx][cury]=1;
int tox, toy;
for(int i = 0; i <4 ; i++){
tox = curx+dx[i], toy = cury+dy[i];
// cout << tox << " " << toy<< " " << curx << " ">>
if(tox > n|| toy >m|| tox <1|| toy < 1)continue;
if(g[tox][toy]==0) continue;
if(g[tox][toy]==5&& (tox!= n ||toy!=m)) continue;
// if(visited[tox][toy]) continue;
ncost =((dir[i]- g[curx][cury])+mod)%mod;
if(ncost + dis[curx][cury] < dis[tox][toy]) {
dis[tox][toy] = ncost+dis[curx][cury];
pq.push({dis[tox][toy],{tox, toy}});
}
}
}
if(dis[n][m]==INT_MAX|| g[1][1]==5) cout << -1<< endl;
else cout << dis[n][m] << endl;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:62:13: warning: unused variable 'cost' [-Wunused-variable]
62 | int cost = pq.top().f;
| ^~~~
adventure.cpp:48:9: warning: unused variable 'ans' [-Wunused-variable]
48 | int ans = INT_MAX;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
392 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
392 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
4 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
4 ms |
2908 KB |
Output is correct |
38 |
Correct |
6 ms |
3420 KB |
Output is correct |
39 |
Correct |
36 ms |
4696 KB |
Output is correct |
40 |
Correct |
36 ms |
4700 KB |
Output is correct |
41 |
Correct |
5 ms |
3812 KB |
Output is correct |
42 |
Correct |
36 ms |
4768 KB |
Output is correct |
43 |
Correct |
63 ms |
10188 KB |
Output is correct |
44 |
Correct |
39 ms |
4692 KB |
Output is correct |