이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
}
cout<<dist[n - 1][m - 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... |