Submission #1295724

#TimeUsernameProblemLanguageResultExecution timeMemory
1295724wolfysAwesome Arrowland Adventure (eJOI19_adventure)C++20
100 / 100
84 ms31796 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector <ll> vi;
typedef pair <ll,ll> pi;
typedef vector <pi> pii;
typedef vector <pii> vii;
#define inf LLONG_MAX
#define F first
#define S second
vii V;






int main () {
    ll N,m;
    cin>>N>>m;
    ll c,e;
    V.resize(N*m);
    //ll A[4]={N,S,W,E};
    for (c=0;c<N;c++) {
        for (e=0;e<m;e++) {
            char a;
            cin>>a;
            if (a=='X') continue;
            ll b;
            if (a=='N') b=0;
            if (a=='E') b=1;
            if (a=='S') b=2;
            if (a=='W') b=3;

            ll pos=c*m+e;

            if (c>0) {
                V[pos].push_back(pi(pos-m, (4-b)%4 ));
            }
            if (c<N-1) {
                V[pos].push_back(pi(pos+m, (6-b)%4) );
            }

            if (e>0) {
                V[pos].push_back(pi(pos-1, (7-b)%4 ));
            }
            if (e<m-1) {
                V[pos].push_back(pi(pos+1, (5-b)%4 ));
            }


        }
    }

    //cout<<1<<endl;


    priority_queue <pi> Q;
    vi dist;
    dist.assign(N*m,inf);
    Q.push({0,0});
    dist[0]=0;


    while (!Q.empty()) {
        pi p=Q.top();
        Q.pop();
        ll a=p.S;
        ll w=-p.F;

        if (w>dist[a]) continue;

        for (ll c=0;c<V[a].size();c++) {
            pi k=V[a][c];
            if (dist[k.F]>k.S+w) {
                dist[k.F]=k.S+w;
                Q.push({-dist[k.F],k.F});
            }
        }
    }


    if (dist[N*m-1]==inf) cout<<-1;
    else cout<<dist[N*m-1];

    return 0;
}
#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...