//don't copy pls)
/*TAAK ZDES NADO RECURSIU PISAT*/
//I'm not in the danger i am the DANGER
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define sigma signed
using namespace std;
using namespace __gnu_pbds;
const int N = 3e5 + 5;
int mod = 1e9 + 7;
const int INF = 1e18;
int n,m,a[501][501],nei[5],dp[501][501],used[501][501];
vector <pii> g[501][501];
int hm(char c , char y){
if(c == 'X' || y == 'X' || (c == y)){
return 0;
}
if(c == 'N'){
if(y == 'E'){
return 1;
}
if(y == 'S'){
return 2;
}
return 3;
}
if(c == 'W'){
if(y == 'N'){
return 1;
}
if(y == 'E'){
return 2;
}
return 3;
}
if(c == 'S'){
if(y == 'w'){
return 1;
}
if(y == 'N'){
return 2;
}
return 3;
}
if(c == 'E'){
if(y == 'S'){
return 1;
}
if(y == 'W'){
return 2;
}
return 3;
}
return 0;
}
char c[501][501];
void dfs(int x , int y){
used[x][y] = 1;
for(auto to : g[x][y]){
int v , u;
v = to.F , u = to.S;
dp[x - 1][y] = min(dp[x - 1][y] , dp[x][y] + hm(c[x][y] , 'N'));
dp[x][y - 1] = min(dp[x][y - 1] , dp[x][y] + hm(c[x][y] , 'W'));
dp[x + 1][y] = min(dp[x + 1][y] , dp[x][y] + hm(c[x][y] , 'S'));
dp[x][y + 1] = min(dp[x][y + 1] , dp[x][y] + hm(c[x][y] , 'E'));
dfs(v , u);
}
}
void Gold(){
cin >> n >> m;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
cin >> c[i][j];
}
}
if(c[n][m] != 'X'){
cout << "-1";
return;
}
if(n == 1){
int cnt = 0;
for(int i = 1 ; i < m ; i++){
if(c[1][i] == 'X'){
cout << "-1";
return;
}
char cc = 'E';
cnt += hm(c[1][i] , cc);
// cout << cnt << ' ';
}
cout << cnt;
return;
}
if(m == 1){
int cnt = 0;
for(int i = 1 ; i < n ; i++){
if(c[i][1] == 'X'){
cout << "-1";
return;
}
char cc = 'S';
cnt += hm(c[i][1] , cc);
// cout << cnt << ' ';
}
cout << cnt;
return;
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
dp[i][j] = INF;
}
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
if(c[i][j] == 'N' && i != 1){
g[i][j].pb({i - 1 , j});
}
if(c[i][j] == 'W' && j != 1){
g[i][j].pb({i , j - 1});
}
if(c[i][j] == 'S' && i != n){
g[i][j].pb({i + 1 , j});
}
if(c[i][j] == 'E' && j != m){
g[i][j].pb({i , j + 1});
}
}
}
dp[1][1] = 0;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
if(dp[i][j] != INF && !used[i][j]){
dfs(i , j);
}
}
}
if(dp[n][m] == INF){
cout << "-1";
}
else{
cout << dp[n][m];
}
}
sigma main(){
//freopen("txt.in","r",stdin);
//freopen("txt.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
int TT = 1;
// cin >> TT;
for(int i = 1 ; i <= TT ; i++){
//cout << "Case " << i << ": ";
Gold();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6236 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
4 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6364 KB |
Output is correct |
9 |
Correct |
3 ms |
6236 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6236 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
4 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6364 KB |
Output is correct |
9 |
Correct |
3 ms |
6236 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6236 KB |
Output is correct |
13 |
Correct |
3 ms |
6120 KB |
Output is correct |
14 |
Correct |
2 ms |
6236 KB |
Output is correct |
15 |
Correct |
3 ms |
6236 KB |
Output is correct |
16 |
Correct |
3 ms |
6236 KB |
Output is correct |
17 |
Correct |
3 ms |
6236 KB |
Output is correct |
18 |
Correct |
3 ms |
6236 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
3 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6232 KB |
Output is correct |
2 |
Runtime error |
67 ms |
102400 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6236 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
4 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6364 KB |
Output is correct |
9 |
Correct |
3 ms |
6236 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6236 KB |
Output is correct |
13 |
Correct |
3 ms |
6120 KB |
Output is correct |
14 |
Correct |
2 ms |
6236 KB |
Output is correct |
15 |
Correct |
3 ms |
6236 KB |
Output is correct |
16 |
Correct |
3 ms |
6236 KB |
Output is correct |
17 |
Correct |
3 ms |
6236 KB |
Output is correct |
18 |
Correct |
3 ms |
6236 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
3 ms |
6236 KB |
Output is correct |
21 |
Incorrect |
3 ms |
6232 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |