#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
#define endl '\n'
using namespace std;
int idk(char a,char b){
int ans=0;
while(a!=b){
ans++;
if(a=='S')a='W';
else if(a=='W')a='N';
else if(a=='N')a='E';
else if(a=='E')a='S';
}
return ans;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
char arr[n][m];
for(int i=0;i<n;i++){
for(int w=0;w<m;w++){
cin>>arr[i][w];
}
}
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;
bool bl[n][m];
memset(bl,false,sizeof(bl));
pq.push({0,{0,0}});
while(pq.size()){
int v=pq.top().first;
int i=pq.top().second.first;
int w=pq.top().second.second;
pq.pop();
if(bl[i][w])continue;
bl[i][w]=true;
if(i==n-1&&w==m-1){cout<<v;return 0;}
if(i>0&&!bl[i-1][w]&&(arr[i-1][w]!='X'||(i-1==n-1&&w==m-1)))pq.push({v+idk(arr[i][w],'N'),{i-1,w}});
if(i<n-1&&!bl[i+1][w]&&(arr[i+1][w]!='X'||(i+1==n-1&&w==m-1)))pq.push({v+idk(arr[i][w],'S'),{i+1,w}});
if(w>0&&!bl[i][w-1]&&(arr[i][w-1]!='X'||(i==n-1&&w-1==m-1)))pq.push({v+idk(arr[i][w],'W'),{i,w-1}});
if(w<m-1&&!bl[i][w+1]&&(arr[i][w+1]!='X'||(i==n-1&&w+1==m-1)))pq.push({v+idk(arr[i][w],'E'),{i,w+1}});
}
cout<<-1;
}
//1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
//0 0 1 0 0 1 0 0 1 0 0 1 0 0 1
//
//1 3 2 4
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Execution timed out |
2086 ms |
204 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Execution timed out |
2084 ms |
204 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Execution timed out |
2086 ms |
204 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |