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 p push
const int maxn = 507;
int kr[4]={-1,0,1,0};
int ks[4]={0,1,0,-1};
int n,m;
char c;
int sve[maxn][maxn];
int dji[maxn][maxn];
priority_queue<pair<int,pair<int,int>> ,vector<pair<int,pair<int,int>>> , greater<pair<int,pair<int,int>>>> q;
int main(){
cin>>n>>m;
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
cin>>c;
if(c=='X'){
sve[i][j]=-1;
}
else if(c=='N'){
sve[i][j]=0;
}
else if(c=='E'){
sve[i][j]=1;
}
else if(c=='S'){
sve[i][j]=2;
}
else if(c=='W'){
sve[i][j]=3;
}
}
}
if(sve[0][0]==-1){
cout<<-1;
return 0;
}
sve[n-1][m-1]=-2;
q.p({0,{0,0}});
pair<int,pair<int,int>> tr;
bool ch = true;
while(!q.empty() and ch){
tr=q.top();
dji[tr.second.first][tr.second.second]=1;
if(tr.second.first==n-1 and tr.second.second==m-1){
cout<<tr.first;
ch=false;
break;
}
int pr = sve[tr.second.first][tr.second.second];
//cout<<tr.first<<" "<<tr.second.first<<' '<<tr.second.second<<" "<<pr<<endl;
q.pop();
for(int i = sve[tr.second.first][tr.second.second]; i<sve[tr.second.first][tr.second.second]+4; i++){
int r = tr.second.first +kr[i%4];
int s = tr.second.second + ks[i%4];
if(r>n-1 or s>m-1 or r<0 or s<0 or dji[r][s]!=0 or sve[r][s]==-1){
continue;
}
else{
//cout<<i<<" "<<r<<' '<<s<<endl;
q.p({tr.first+(i-pr),{r,s}});
}
}
}
if(ch) cout<<-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... |