이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct ano{
int c;
int d;
bool operator<(const ano &o)const{
return d>o.d;
}
};
vector<vector<ano> > a(250000);
priority_queue<ano> q;
int d[250000];
int main (void){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
int i,j;
char c;
for(i=0;i<n*m;i++)
d[i]=1000000;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
cin>>c;
if(c=='N'){
if(i-1>=0)
a[i*m+j].push_back({(i-1)*m+j,0});
if(j+1<m)
a[i*m+j].push_back({i*m+j+1,1});
if(i+1<n)
a[i*m+j].push_back({(i+1)*m+j,2});
if(j-1>=0)
a[i*m+j].push_back({i*m+j-1,3});
}
else if(c=='E'){
if(i-1>=0)
a[i*m+j].push_back({(i-1)*m+j,3});
if(j+1<m)
a[i*m+j].push_back({i*m+j+1,0});
if(i+1<n)
a[i*m+j].push_back({(i+1)*m+j,1});
if(j-1>=0)
a[i*m+j].push_back({i*m+j-1,2});
}
else if(c=='S'){
if(i-1>=0)
a[i*m+j].push_back({(i-1)*m+j,2});
if(j+1<m)
a[i*m+j].push_back({i*m+j+1,3});
if(i+1<n)
a[i*m+j].push_back({(i+1)*m+j,0});
if(j-1>=0)
a[i*m+j].push_back({i*m+j-1,1});
}
else if(c=='W'){
if(i-1>=0)
a[i*m+j].push_back({(i-1)*m+j,1});
if(j+1<m)
a[i*m+j].push_back({i*m+j+1,2});
if(i+1<n)
a[i*m+j].push_back({(i+1)*m+j,3});
if(j-1>=0)
a[i*m+j].push_back({i*m+j-1,0});
}
}
q.push({0,0});
d[0]=0;
int start,w;
while(!q.empty()){
start=q.top().c;
w=q.top().d;
q.pop();
if(d[start]<w)
continue;
for(auto x : a[start])
if(d[x.c]>d[start]+x.d){
d[x.c]=d[start]+x.d;
q.push(x);
}
}
if(d[n*m-1]==1000000)
cout<<-1<<endl;
else
cout<<d[n*m-1]<<endl;
}
# | 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... |