이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 200001
#define INF 1000000001
#define MID ((l+r)/2)
#define RANGE (r-l+1)
int main() {
ll n, m;
cin>>n>>m;
char c;
ll u[n][m], r[n][m], d[n][m], l[n][m];
for(ll i=0; i<n; i++){
for(ll j=0; j<m; j++){
cin>>c;
if(c=='N'){
u[i][j]=0;
r[i][j]=1;
d[i][j]=2;
l[i][j]=3;
}
else if(c=='E'){
u[i][j]=3;
r[i][j]=0;
d[i][j]=1;
l[i][j]=2;
}
else if(c=='S'){
u[i][j]=2;
r[i][j]=3;
d[i][j]=0;
l[i][j]=1;
}
else if(c=='W'){
u[i][j]=1;
r[i][j]=2;
d[i][j]=3;
l[i][j]=0;
}
else{
u[i][j]=INF;
r[i][j]=INF;
d[i][j]=INF;
l[i][j]=INF;
}
if(i==0){
u[i][j]=INF;
}
else if(i==n-1){
d[i][j]=INF;
}
if(j==0){
l[i][j]=INF;
}
else if(j==m-1){
r[i][j]=INF;
}
}
}
priority_queue <pair<ll,pair<ll,ll>>> q; q.push({0,{0,0}});
ll dist[n][m];
for(ll i=0; i<n; i++){
for(ll j=0; j<m; j++){
dist[i][j]=INF;
}
}
dist[0][0]=0;
ll pr[n][m]={};
while(!q.empty()){
ll i=q.top().second.first;
ll j=q.top().second.second;
q.pop();
if(pr[i][j]){
continue;
}
pr[i][j]=true;
if(i>0 && dist[i][j]+u[i][j]<dist[i-1][j]){
dist[i-1][j]=dist[i][j]+u[i][j];
q.push({-(dist[i][j]+u[i][j]),{i-1,j}});
}
if(i<n-1 && dist[i][j]+d[i][j]<dist[i+1][j]){
dist[i+1][j]=dist[i][j]+d[i][j];
q.push({-(dist[i][j]+d[i][j]),{i+1,j}});
}
if(j>0 && dist[i][j]+l[i][j]<dist[i][j-1]){
dist[i][j-1]=dist[i][j]+l[i][j];
q.push({-(dist[i][j]+l[i][j]),{i,j-1}});
}
if(j<m-1 && dist[i][j]+r[i][j]<dist[i][j+1]){
dist[i][j+1]=dist[i][j]+r[i][j];
q.push({-(dist[i][j]+r[i][j]),{i,j+1}});
}
// for(ll i1=0; i1<n; i1++){
// for(ll j1=0; j1<m; j1++){
// cout<<setw(3)<<((dist[i1][j1]<INF)?dist[i1][j1]:-1);
// }
// cout<<endl;
// }
// cout<<endl<<q.top().first<<" "<<q.top().second.first<<" "<<q.top().second.second<<endl;
}
cout<<((dist[n-1][m-1]<INF)?dist[n-1][m-1]:-1);
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... |