This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 505 //PROMENI OVO OBAVEZNO AKO TREBA
#define fi first
#define se second
#define mod 1000000007LL
#define endl "\n"
#define pll pair<long long,long long>
#define PRINT(x) cout<<#x<<"-"<<x<<endl
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
int dist[MAXN][MAXN];
int arrow[MAXN][MAXN];
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1},n,m;
int okretanje[4][4]={{0, 1, 2, 3},{3, 0, 1, 2},{2, 3, 0, 1},{1, 2, 3, 0}};
void dijkstra(){
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
dist[i][j]=-1;
priority_queue<pair<ll,pll>,vector<pair<ll,pll>>,greater<pair<ll,pll>>> pq;
pq.push({0,{0,0}});
while(!pq.empty()){
int x=pq.top().se.fi, y=pq.top().se.se, d=pq.top().fi;
pq.pop();
if(x<0 || x>=n || y<0 || y>=m || dist[x][y]!=-1) continue;
dist[x][y]=d;
if(arrow[x][y]==-1) continue;
for(int i=0;i<4;i++){
pq.push({d+okretanje[arrow[x][y]][i],{x+dx[i],y+dy[i]}});
}
}
}
void solve(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
char c; cin>>c;
if(c=='N') arrow[i][j]=0;
if(c=='E') arrow[i][j]=1;
if(c=='S') arrow[i][j]=2;
if(c=='W') arrow[i][j]=3;
if(c=='X') arrow[i][j]=-1;
}
}
dijkstra();
cout<<dist[n-1][m-1];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int t;
//cin>>t;
t=1;
while(t--){
solve();
}
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... |