#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
using namespace std;
char A[505][505];
int dp[505][505],n,m;
bool f[505][505],q,F[505][505];
vector <char> v = {'N','E','S','W'};
int go(char x,char y){
int lf = -1,rg = -1;
for (int i=0;i<v.size();i++){
if (v[i] == x) lf = i;
if (v[i] == y) rg = i;
}
if (x == 'X')
return 1e9;
if (lf <= rg)
return rg - lf;
return 4 - lf + rg;
}
int get(int x,int y){
// if (A[x][y] == 'X' && !(x == n - 1 && y == n - 1))
// return 1e9;
if (x < 0 || x >= n || y < 0 || y >= m)
return 1e9;
if (F[x][y])
return dp[x][y] = 0;
if (dp[x][y] != -1)
return dp[x][y];
if (f[x][y])
return 1e9;
f[x][y] = 1;
int a = get(x - 1,y) + go(A[x - 1][y],'S');
int b = get(x + 1,y) + go(A[x + 1][y],'N');
int c = get(x,y - 1) + go(A[x][y - 1],'E');
int d = get(x,y + 1) + go(A[x][y + 1],'W');
return dp[x][y] = min({a,b,c,d});
}
void G(int x,int y){
if (x < 0 || x >= n || y < 0 || y >= m)
return;
F[x][y] = 1;
if (A[x][y] == 'N') G(x-1,y);
if (A[x][y] == 'S') G(x+1,y);
if (A[x][y] == 'E') G(x,y+ 1);
if (A[x][y] == 'W') G(x,y-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] = -1;
G(0,0);
cout<<get(n - 1,m - 1)<<"\n";
}
Compilation message
adventure.cpp: In function 'int go(char, char)':
adventure.cpp:19:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
59 ms |
102404 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |