제출 #750212

#제출 시각아이디문제언어결과실행 시간메모리
750212emad234Awesome Arrowland Adventure (eJOI19_adventure)C++17
100 / 100
61 ms6016 KiB
#include <bits/stdc++.h>
#define all(v) ((v).begin(),(v).end())
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const ll mxN = 4e6 + 5;
int n,m;
struct node{
  int i,j,cost;
  char dir;
};
bool operator<(node a,node b){
  return a.cost < b.cost;
}
bool operator>(node a,node b){
  return a.cost > b.cost;
}
int dist[520][520];
int dp[520][520];
char a[520][520];
bool valid(int i,int j){
  return !(i < 0 || i >= n || j < 0 || j >= m);
}
node psh(node u,int x,int y,char dire){
  int val,val1;
  if(dire == 'N') val = 1;
  if(dire == 'E') val = 2;
  if(dire == 'S') val = 3;
  if(dire == 'W') val = 4;
  if(u.dir == 'N') val1 = 1;
  if(u.dir == 'E') val1 = 2;
  if(u.dir == 'S') val1 = 3;
  if(u.dir == 'W') val1 = 4;
  int sum = val - val1;
  if(sum < 0) sum += 4;
  if(!valid(u.i+ x,u.j + y)) return {-1,-1,-1,'Z'};
  if(dist[u.i + x][u.j + y] > u.cost + sum) {
    dist[u.i + x][u.j + y] = u.cost + sum;
    return{u.i + x,u.j + y,dist[u.i + x][u.j + y],a[u.i + x][u.j + y]};
  }
  return {-1,-1,-1,'Z'};
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);cout.tie(0);
  cin >>n>>m;
  for(int i= 0 ;i < n;i++){
    for(int j = 0;j < m;j++){
      cin >>a[i][j];
      dist[i][j] = INT_MAX;
    }
  }
  dist[0][0] = 0;
  priority_queue<node,vector<node>,greater<node>>q;
  q.push({0,0,0,a[0][0]});
  while(q.size()){
    node u = q.top();
    // cout<<u.i<<' '<<u.j<<' '<<u.cost<<' '<<u.dir<<'\n';
    q.pop();
    if(u.dir == 'X') continue;
    if(dist[u.i][u.j] < u.cost) continue;
    node v = psh(u,-1,0,'N');
    if(v.i != -1) q.push(v);
    v = psh(u,1,0,'S');
    if(v.i != -1) q.push(v);
    v = psh(u,0,1,'E');
    if(v.i != -1) q.push(v);
    v = psh(u,0,-1,'W');
    if(v.i != -1) q.push(v);
  }
  if(dist[n - 1][m - 1] == INT_MAX) dist[n - 1][m - 1] = -1;
  cout<<dist[n - 1][m - 1];

}
#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...