Submission #445692

#TimeUsernameProblemLanguageResultExecution timeMemory
445692aris12345678Awesome Arrowland Adventure (eJOI19_adventure)C++14
100 / 100
117 ms9036 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int mxN = 505;
char str[mxN][mxN];
ll dist[mxN][mxN];
string pos = "NESW";
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

bool check(int i, int j, int n, int m) {
    return i >= 0 && j >= 0 && i < n && j < m;
}

int main() {
    int n, m;
    scanf("%d %d\n", &n, &m);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> str[i][j];
            dist[i][j] = LLONG_MAX;
        }
    }
    // cout << str[2][2] << "\n";
    dist[0][0] = 0;
    // (distance, i, j)
    priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll> >, greater<tuple<ll, ll, ll> > > pq;
    pq.push({0, 0, 0});
    while(!pq.empty()) {
        ll i = get<1>(pq.top()), j = get<2>(pq.top()), w = get<0>(pq.top());
        // cout << i << " " << j << " " << w << "\n"; 
        pq.pop();
        if(dist[i][j] != w) continue;
        if(str[i][j] == 'X') continue;
        ll direction = pos.find(str[i][j]);
        // cout << str[i][j] << " " << direction << "\n"; 
        for(int k = 0; k < 4; k++) {
            int x = i+dx[k], y = j+dy[k];
            // cout << x << " " << y << "\n";
            if(check(x, y, n, m)) {
                ll weight = (k-direction+4)%4;
                // cout << dist[x][y] << " " << dist[i][j] << " " << weight << "\n";
                if(dist[x][y] > dist[i][j]+weight) {
                    dist[x][y] = dist[i][j]+weight;
                    pq.push({dist[x][y], x, y});
                }
            }
        }
    }
    if(dist[n-1][m-1] == LLONG_MAX)
        printf("-1\n");
    else
        printf("%lld\n", dist[n-1][m-1]);
    return 0;
}

Compilation message (stderr)

adventure.cpp: In function 'int main()':
adventure.cpp:18:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |     scanf("%d %d\n", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
#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...