#include<bits/stdc++.h>
using namespace std;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
const int LIM=507, INF=1e9+7;
string x[LIM];
int odl[LIM][LIM];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
rep(i, n) cin >> x[i];
deque<pair<pair<int,int>,int>>q;
rep(i, n) rep(j, m) odl[i][j]=INF;
q.push_front({{0, 0}, 0});
while(!q.empty()) {
int a=q.front().st.st, b=q.front().st.nd, o=q.front().nd;
q.pop_front();
if(o>=odl[a][b]) continue;
odl[a][b]=o;
if(a) {
if(x[a][b]=='N') q.push_front({{a-1, b}, o});
else if(x[a][b]=='W') q.push_back({{a-1, b}, o+1});
else if(x[a][b]=='S') q.push_back({{a-1, b}, o+2});
else if(x[a][b]=='E') q.push_back({{a-1, b}, o+3});
}
if(a<n-1) {
if(x[a][b]=='S') q.push_front({{a+1, b}, o});
else if(x[a][b]=='E') q.push_back({{a+1, b}, o+1});
else if(x[a][b]=='N') q.push_back({{a+1, b}, o+2});
else if(x[a][b]=='W') q.push_back({{a+1, b}, o+3});
}
if(b) {
if(x[a][b]=='W') q.push_front({{a, b-1}, o});
else if(x[a][b]=='S') q.push_back({{a, b-1}, o+1});
else if(x[a][b]=='E') q.push_back({{a, b-1}, o+2});
else if(x[a][b]=='N') q.push_back({{a, b-1}, o+3});
}
if(b<m-1) {
if(x[a][b]=='E') q.push_front({{a, b+1}, o});
else if(x[a][b]=='N') q.push_back({{a, b+1}, o+1});
else if(x[a][b]=='W') q.push_back({{a, b+1}, o+2});
else if(x[a][b]=='S') q.push_back({{a, b+1}, o+3});
}
}
cout << odl[n-1][m-1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |