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>
#define MAXN 507
using namespace std;
int ind[MAXN][MAXN],dist[MAXN][MAXN],dx[4]={0,1,0,-1},dy[4]={-1,0,1,0},n,m;
void dijkstra()
{
for(int i=0;i<n;i++) for(int j=0;j<m;j++) dist[i][j]=-1;
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>> >, greater<pair<int,pair<int,int> > > > pq;
pq.push(make_pair(0,make_pair(0,0)));
while(!pq.empty())
{
int x=pq.top().second.first,y=pq.top().second.second,c=pq.top().first;
pq.pop();
if(x<0 || x>=m || y<0 || y>=n || dist[y][x]!=-1) continue;
dist[y][x]=c;
if(ind[y][x]==-1) continue;
for(int i=0;i<4;i++) pq.push(make_pair(c+i,make_pair(x+dx[(ind[y][x]+i)%4],y+dy[(ind[y][x]+i)%4])));
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++) for(int j=0;j<m;j++)
{
char c; cin>>c;
if(c=='N') ind[i][j]=0;
if(c=='E') ind[i][j]=1;
if(c=='S') ind[i][j]=2;
if(c=='W') ind[i][j]=3;
if(c=='X') ind[i][j]=-1;
}
dijkstra();
cout<<dist[n-1][m-1];
}
# | 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... |