Submission #884731

#TimeUsernameProblemLanguageResultExecution timeMemory
884731Perl32Awesome Arrowland Adventure (eJOI19_adventure)C++14
100 / 100
79 ms19152 KiB
/**
 *    author:  Perl32
 *    created: 18.11.2023 09:12:21
**/

#ifdef LOCAL
#  define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ff first
#define ss second
#define szof(x) (int) x.size()
#define all(x) x.begin(), x.end()
#ifndef LOCAL
#  define cerr if (0) cerr
#endif

using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using ull = unsigned long long;

template<typename T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

const int INF = (int) 1e9 + 1e3;
const ll INFL = (ll) 1e18 + 1e9;
#ifdef LOCAL
    mt19937 tw(9450189);
#else
    mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

signed main() {
#ifdef LOCAL
    freopen("inp.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("err.txt", "w", stderr);
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    int dx[4] = {-1, 0, 1, 0};
    int dy[4] = {0, 1, 0, -1};
    auto valid = [&](int x, int y) {
        return x > -1 && x < n && y > -1 && y < m;
    };
    vector<vector<pii>> g(n * m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            char c;
            cin >> c;
            if (c == 'X') {
                continue;
            }
            int st;
            if (c == 'W') {
                st = 3;
            } else if (c == 'E') {
                st = 1;
            } else if (c == 'N') {
                st = 0;
            } else {
                st = 2;
            }
            for (int k = 0; k < 4; ++k) {
                int x = i + dx[(st + k) % 4], y = j + dy[(st + k) % 4];
                if (valid(x, y)) {
                    g[i * m + j].emplace_back(x * m + y, k);
                }
            }
        }
    }
    set<pii> que;
    vector<int> dist(n * m, INF);
    dist[0] = 0;
    que.insert({0, 0});
    while (!que.empty()) {
        auto [d, u] = *que.begin();
        que.erase(que.begin());
        for (auto [to, w] : g[u]) {
            if (dist[to] > dist[u] + w) {
                que.erase({dist[to], to});
                dist[to] = dist[u] + w;
                que.insert({dist[to], to});
            }
        }
    }
    if (dist[n * m - 1] == INF) {
        cout << -1;
        return 0;
    }
    cout << dist[n * m - 1];

#ifdef LOCAL
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (ll) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}

/*

 */

Compilation message (stderr)

adventure.cpp: In function 'int main()':
adventure.cpp:87:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   87 |         auto [d, u] = *que.begin();
      |              ^
adventure.cpp:89:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   89 |         for (auto [to, w] : g[u]) {
      |                   ^
#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...