#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstring>
#include <unordered_map>
using namespace std;
typedef long long ll;
ll dp[507][507];
string a[507];
char rotate(char c){
if(c == 'N')
return 'E';
if(c == 'E')
return 'S';
if(c == 'S')
return 'W';
return 'N';
}
pair<ll, ll> move(ll r, ll c, char v){
if(v == 'N')
return {r - 1, c};
if(v == 'E')
return {r, c + 1};
if(v == 'S')
return {r + 1, c};
return {r, c - 1};
}
ll n, m;
void count_dp(ll r, ll c, ll step){
if(r < 1 || c < 1 || r > n || c > m || dp[r][c] <= step){
return;
}
//cout << r << ' ' << c << endl;
if(a[r][c] == 'X'){
if(r == n && c == m)
dp[r][c] = step;
return;
}
dp[r][c] = step;
char v = a[r][c];
for(int i = 0; i < 4; i++){
auto y = move(r, c, v);
// cout << r << ' ' << c << ' ' << y.first << ' ' << y.second << " " << step + i << endl;
count_dp(y.first, y.second, step + i);
v = rotate(v);
}
}
ll cost(char a, char b){
for(int i = 0; i < 4; i++){
if(a == b)
return i;
a = rotate(a);
}
return 0;
}
ll n2(){
vector<vector<ll>>dp2(n + 1, vector<ll>(m + 1, 1e9));
dp2[1][1] = (a[1][1] == 'X' ? 1e9 : 0);
dp2[2][1] = (a[2][1] == 'X' ? 1e9 : cost(a[1][1], 'S'));
for(int j = 2; j <= m; j++){
for(int i = 1; i <= n; i++){
if(a[i][j] == 'X' && (i != n || j != m))
continue;
// go from left:
dp2[i][j] = dp2[i][j - 1] + cost(a[i][j - 1], 'E');
ll cnt = 0;
if(i == 1){ // go from (i + 1, j)
cnt = dp2[i + 1][j - 1] + cost(a[i + 1][j - 1], 'E') + cost(a[i + 1][j], 'N');
}
else{
cnt = dp2[i - 1][j - 1] + cost(a[i - 1][j - 1], 'E') + cost(a[i - 1][j], 'S');
}
dp2[i][j] = min(dp2[i][j], cnt);
}
}
if(dp2[n][m] >= 1e9)
dp2[n][m] = -1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++)
cout << dp2[i][j] << ' ';
cout << endl;
}
return dp2[n][m];
}
int main(){
srand(time(0));
// cin >> n >> m;
// ll t = 10000;
ll t = 1;
while(t--){
//n = 2, m = 1 + rand() % 12;
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
/*
for(int j = 1; j <= m; j++){
ll c = rand() % 4;
if(c == 0)
a[i] += 'N';
else if(c == 1)
a[i] += 'E';
else if(c == 2)
a[i] += 'S';
else
a[i] += 'W';
}
*/
a[i] = "#" + a[i];
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
dp[i][j] = 1e9;
count_dp(1, 1, 0);
if(dp[n][m] == (ll)(1e9))
dp[n][m] = -1;
cout << dp[n][m] << endl;
return 0;
if(dp[1][1] == (ll)(1e18) || dp[1][1] == -2)
dp[1][1] = -1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cout << dp[i][j] << ' ';
if(i == 1 && j >= 5 && j <= 7)
cout << ' ';
}
cout << endl;
}
cout << endl;
if(dp[1][1] != n2()){
cout << dp[1][1] << ' ' << n2() << endl;
cout << n << ' ' << m << endl;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++)
cout << a[i][j];
cout << endl;
}
return 0;
}
}
return 0;
}
/*
2 13
SSNWNEWESEWWE
WSENESSWNNSWW
*/
/*
2 11
W W W E S N N S S E W
W E E N W W E S S W W
*/
/*
2 5
EXNWS
SEEXX
*/
/*
2 4
EWEW
EWXX
*/
/*
2 4
EWEW
EWEX
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
324 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 |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
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 |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
324 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 |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
328 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Execution timed out |
2058 ms |
816 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |