#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pi pair<int,int>
#define s second
#define f first
#define left (h<<1),l,((l+r)>>1)
#define right ((h<<1)|1),((l+r)>>1) + 1,r
#define int ll
using namespace std;
char A[505][505];
int dp[505][505],n,m;
bool f[505][505];
priority_queue <pair<int,pi> > q;
vector <char> v = {'N','E','S','W'};
int go(char x,char y){
if (x == 'X') return 1e9;
int lf = 0,rg = 0;
for (int i = 0; i < v.size(); i++){
if (v[i] == x) lf = i;
if (v[i] == y) rg = i;
}
if (lf <= rg) return rg - lf;
return 4 - lf + rg;
}
bool check(int x,int y){
if (x < 0 || x >= n || y < 0 || y >= m)
return 0;
return 1;
}
signed main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
cin>>n>>m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> A[i][j],dp[i][j] = 1e9;
q.push({0,{0,0}});
dp[0][0] = 0;
while(q.size()){
int x = q.top().s.f,y = q.top().s.s,d = -q.top().f;
q.pop();
if (dp[x][y] != d) continue;
if (check(x - 1,y)){
int a = d + go(A[x][y],'N');
if (dp[x - 1][y] > a){
dp[x - 1][y] = a;
q.push({-a,{x - 1,y}});
}
}
if (check(x + 1,y)){
int b = d + go(A[x][y],'S');
if (dp[x + 1][y] > b){
dp[x + 1][y] = b;
q.push({-b,{x + 1,y}});
}
}
if (check(x,y - 1)){
int c = d + go(A[x][y],'W');
if (dp[x][y - 1] > c){
dp[x][y - 1] = c;
q.push({-c,{x,y - 1}});
}
}
if (check(x,y + 1)){
int k = d + go(A[x][y],'E');
if (dp[x][y + 1] > k){
dp[x][y + 1] = k;
q.push({-k,{x,y + 1}});
}
}
}
ll ans = dp[n - 1][m - 1];
if (ans >= 1e9) cout<<-1<<"\n";
else cout<<ans<<"\n";
}
Compilation message
adventure.cpp: In function 'long long int go(char, char)':
adventure.cpp:21:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
316 KB |
Output is correct |
34 |
Correct |
0 ms |
332 KB |
Output is correct |
35 |
Correct |
6 ms |
620 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
6 ms |
844 KB |
Output is correct |
38 |
Correct |
2 ms |
1484 KB |
Output is correct |
39 |
Correct |
61 ms |
2836 KB |
Output is correct |
40 |
Correct |
62 ms |
2856 KB |
Output is correct |
41 |
Correct |
6 ms |
2764 KB |
Output is correct |
42 |
Correct |
60 ms |
2848 KB |
Output is correct |
43 |
Correct |
120 ms |
9060 KB |
Output is correct |
44 |
Correct |
6 ms |
2764 KB |
Output is correct |