This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
vector<pair<pair<int,int>,int>>V[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];
priority_queue<pair<int,pair<int,int>>>q;
rep(i, n) rep(j, m) odl[i][j]=INF;
q.push({0, {0, 0}});
while(!q.empty()) {
int a=q.top().nd.st, b=q.top().nd.nd, o=-q.top().st;
q.pop();
if(o>=odl[a][b]) continue;
odl[a][b]=o;
if(x[a][b]=='X') continue;
if(a) {
if(x[a][b]=='N') q.push({-o, {a-1, b}});
else if(x[a][b]=='W') q.push({-o-1, {a-1, b}});
else if(x[a][b]=='S') q.push({-o-2, {a-1, b}});
else if(x[a][b]=='E') q.push({-o-3, {a-1, b}});
}
if(a<n-1) {
if(x[a][b]=='S') q.push({-o, {a+1, b}});
else if(x[a][b]=='E') q.push({-o-1, {a+1, b}});
else if(x[a][b]=='N') q.push({-o-2, {a+1, b}});
else if(x[a][b]=='W') q.push({-o-3, {a+1, b}});
}
if(b) {
if(x[a][b]=='W') q.push({-o, {a, b-1}});
else if(x[a][b]=='S') q.push({-o-1, {a, b-1}});
else if(x[a][b]=='E') q.push({-o-2, {a, b-1}});
else if(x[a][b]=='N') q.push({-o-3, {a, b-1}});
}
if(b<m-1) {
if(x[a][b]=='E') q.push({-o, {a, b+1}});
else if(x[a][b]=='N') q.push({-o-1, {a, b+1}});
else if(x[a][b]=='W') q.push({-o-2, {a, b+1}});
else if(x[a][b]=='S') q.push({-o-3, {a, b+1}});
}
}
cout << odl[n-1][m-1] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |