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;
char arr[1000][1000];
long long n, m;
int dist(char a, char b){
if(a=='N'){
if(b=='N')
return 0;
else if(b=='E')
return 1;
else if(b=='S')
return 2;
else
return 3;
}
else if(a=='E'){
if(b=='N')
return 3;
else if(b=='E')
return 0;
else if(b=='S')
return 1;
else
return 2;
}
else if(a=='S'){
if(b=='N')
return 2;
else if(b=='E')
return 3;
else if(b=='S')
return 0;
else
return 1;
}
else{
if(b=='N')
return 1;
else if(b=='E')
return 2;
else if(b=='S')
return 3;
else
return 0;
}
}
long long recursive(long long hi1, long long hi2, long long yo){
if(hi1<0 || hi1>=n || hi2<0 || hi2>=m)
return 1000000000;
if(hi1==n-1 && hi2==m-1)
return 0;
if(arr[hi1][hi2]=='X' || yo==0)
return 1000000000;
long long mini=2e9;
char asdf=arr[hi1][hi2];
mini=min(mini, recursive(hi1+1, hi2, yo-1)+dist(asdf, 'S'));
mini=min(mini, recursive(hi1-1, hi2, yo-1)+dist(asdf, 'N'));
mini=min(mini, recursive(hi1, hi2+1, yo-1)+dist(asdf, 'E'));
mini=min(mini, recursive(hi1, hi2-1, yo-1)+dist(asdf, 'W'));
return mini;
}
int main(){
cin>>n>>m;
for(long long i=0; i<n; i++){
for(long long j=0; j<m; j++){
cin>>arr[i][j];
}
}
cout<<recursive(0, 0, n*m);
return 0;
}
# | 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... |