제출 #750236

#제출 시각아이디문제언어결과실행 시간메모리
750236MrM7mdAwesome Arrowland Adventure (eJOI19_adventure)C++17
50 / 100
2076 ms1876 KiB
#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[501][501];
int val[501][501];

int dist(int x,int y,int i,int j){
   // if(ch[i][j]=='X')return INT_MAX;
   // cout<<x<<' '<<y<<' '<<i<<' '<<j<<endl;
   if(x==i-1){
      if(ch[x][y]=='E')return 1;
      if(ch[x][y]=='N')return 2;
      if(ch[x][y]=='W')return 3;
      return 0;
   }
   if(x==i+1){
      if(ch[x][y]=='W')return 1;
      if(ch[x][y]=='S')return 2;
      if(ch[x][y]=='E')return 3;
      return 0;
   }if(y==j+1){
      if(ch[x][y]=='S')return 1;
      if(ch[x][y]=='E')return 2;
      if(ch[x][y]=='N')return 3;
      return 0;
   }if(y==j-1){
      if(ch[x][y]=='N')return 1;
      if(ch[x][y]=='W')return 2;
      if(ch[x][y]=='S')return 3;
      return 0;
   }
   return 1e9;
}
void dijkstra(int x,int y){
   if(ch[x][y]=='X')return ;

   if(val[x-1][y]>(val[x][y]+dist(x,y,x-1,y)) ){
      val[x-1][y]=val[x][y]+dist(x,y,x-1,y);
      dijkstra(x-1,y);
   }
   if(val[x+1][y]>(val[x][y]+dist(x,y,x+1,y)) ){
      val[x+1][y]=val[x][y]+dist(x,y,x+1,y);
      dijkstra(x+1,y);
   }
   if(val[x][y-1]>(val[x][y]+dist(x,y,x,y-1)) ){
      val[x][y-1]=val[x][y]+dist(x,y,x,y-1);
      dijkstra(x,y-1);
   }
   if(val[x][y+1]>(val[x][y]+dist(x,y,x,y+1)) ){
      // cout<<dist(x,y,x,y+1)<<'\n';
      val[x][y+1]=val[x][y]+dist(x,y,x,y+1);
      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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...