Submission #457700

#TimeUsernameProblemLanguageResultExecution timeMemory
457700SamAndAwesome Arrowland Adventure (eJOI19_adventure)C++17
28 / 100
1 ms332 KiB
#include <bits/stdc++.h> using namespace std; #define m_p make_pair #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define fi first #define se second typedef long long ll; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); const int N = 502; const int xx[4] = {0, 1, 0, -1}; const int yy[4] = {1, 0, -1, 0}; const char cc[4] = {'E', 'S', 'W', 'N'}; int n, m; char a[N][N]; struct ban { int x, y, d; ban(){} ban(int x, int y, int d) { this->x = x; this->y = y; this->d = d; } }; bool operator<(const ban& a, const ban& b) { return a.d > b.d; } bool c[N][N]; void solv() { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> (a[i] + 1); priority_queue<ban> q; q.push(ban(n, m, 0)); while (!q.empty()) { ban t; do { if (q.empty()) { cout << "-1\n"; return; } t = q.top(); q.pop(); } while (c[t.x][t.y]); c[t.x][t.y] = true; if (t.x == 1 && t.y == 1) { cout << t.d << "\n"; return; } for (int i = 0; i < 4; ++i) { int hx = t.x + xx[i]; int hy = t.y + yy[i]; if (1 <= hx && hx <= n && 1 <= hy && hy <= m) { int j = (i + 2) % 4; int u = -1; for (int k = 0; k < 4; ++k) { if (a[hx][hy] == cc[k]) { u = k; break; } } if (u != -1) { int dd; if (u <= j) dd = j - u; else dd = j + 4 - u; q.push(ban(hx, hy, t.d + dd)); } } } } } int main() { #ifdef SOMETHING freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif // SOMETHING ios_base::sync_with_stdio(false), cin.tie(0); int tt = 1; //cin >> tt; while (tt--) { solv(); } 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...