Submission #459304

#TimeUsernameProblemLanguageResultExecution timeMemory
459304lukadupliAwesome Arrowland Adventure (eJOI19_adventure)C++14
100 / 100
277 ms18244 KiB
#include <bits/stdc++.h>

#define f first
#define s second

using namespace std;

const int MAX = 505, INF = 2e9;

typedef pair <int, int> pii;

int n, m, mat[MAX][MAX], sol[MAX][MAX];

set <pair <int, pii>> dijk;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int main()
{
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        string row;
        cin >> row;

        for(int j = 0; j < m; j++){
            if(row[j] == 'N') mat[i][j] = 0;
            else if(row[j] == 'E') mat[i][j] = 1;
            else if(row[j] == 'S') mat[i][j] = 2;
            else if(row[j] == 'W') mat[i][j] = 3;
            else mat[i][j] = -1;

            if(i > 0 || j > 0){
                sol[i][j] = INF;
                dijk.insert({INF, {i, j}});
            }
        }
    }
    dijk.insert({0, {0, 0}});

    while(!dijk.empty()){
        auto now = *dijk.begin();
        dijk.erase(now);

        int x = now.s.f, y = now.s.s;
        if(mat[x][y] == -1) continue;

        for(int i = 0; i < 4; i++){
            int nx = x + dx[i], ny = y + dy[i];

            if(nx >= 0 && nx < n && ny >= 0 && ny < m){
                int new_val = sol[x][y];

                if(i >= mat[x][y]) new_val += (i - mat[x][y]);
                else new_val += (i + 4 - mat[x][y]);

                if(new_val < sol[nx][ny]){
                    dijk.erase({sol[nx][ny], {nx, ny}});
                    sol[nx][ny] = new_val;
                    dijk.insert({sol[nx][ny], {nx, ny}});
                }
            }
        }

        /*cout << x << ' ' << y << '\n';

        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++) cout << sol[i][j] << ' ';
            cout << '\n';
        }

        cout << '\n';*/
    }

    if(sol[n - 1][m - 1] >= INF) cout << -1;
    else cout << sol[n - 1][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...