#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;
ll count_dp(ll r, ll c){
if(r < 1 || c < 1 || r > n || c > m || dp[r][c] == -2 || (a[r][c] == 'X' && (r != n || c != m))){ // cycle
return -1;
}
if(r == n && c == m){
return dp[r][c] = 0;
}
if(dp[r][c] != -1)
return dp[r][c];
ll mn = 1e18;
dp[r][c] = -2;
char v = a[r][c];
ll can = 0;
for(int i = 0; i < 4; i++){
auto y = move(r, c, v);
ll cnt = count_dp(y.first, y.second);
if(cnt != -1){
mn = min(mn, i + cnt);
can++;
}
v = rotate(v);
}
if(can == 0){
mn = -1;
return dp[r][c] = mn;
}
return dp[r][c] = mn;
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
a[i] = "#" + a[i];
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
dp[i][j] = -1;
count_dp(1, 1);
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] << ' ';
cout << endl;
}
cout << dp[1][1] << endl;
return 0;
}
/*
2 4
EWEW
EWXX
*/
/*
2 4
EWEW
EWEX
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |