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 int long long
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(a) a.begin(),a.end()
const int N=1e6+500;
const int off=(1<<18);
const int MOD = 1e9+7;
char ch[502][502];
int val[502][502];
void dijkstra(int x,int y){
if(ch[x][y]=='X')return ;
int l=INT_MAX,r=INT_MAX,u=INT_MAX,d=INT_MAX;
if(ch[x][y]=='E')r=0,l=2,u=3,d=1;
if(ch[x][y]=='N')r=1,l=3,u=0,d=2;
if(ch[x][y]=='W')r=2,l=0,u=1,d=3;
if(ch[x][y]=='S')r=3,l=1,u=2,d=0;
if(val[x-1][y]>(val[x][y]+u) ){
val[x-1][y]=val[x][y]+u;
dijkstra(x-1,y);
}
if(val[x+1][y]>(val[x][y]+d) ){
val[x+1][y]=val[x][y]+d;
dijkstra(x+1,y);
}
if(val[x][y-1]>(val[x][y]+l) ){
val[x][y-1]=val[x][y]+l;
dijkstra(x,y-1);
}
if(val[x][y+1]>(val[x][y]+r) ){
// cout<<dist(x,y,x,y+1)<<'\n';
val[x][y+1]=val[x][y]+r;
dijkstra(x,y+1);
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,m;
cin >> m >> n;
memset(val,-1,sizeof(val));
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
val[i][j]=1e9;
cin >> ch[i][j];
}
}
val[1][1]=0;
dijkstra(1,1);
if(val[m][n]==1e9)val[m][n]=-1;
cout<<val[m][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... |