Submission #964702

#TimeUsernameProblemLanguageResultExecution timeMemory
964702rahidilbayramliAwesome Arrowland Adventure (eJOI19_adventure)C++17
50 / 100
2050 ms9584 KiB
#pragma GCC optimize("-O3") #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define vl vector<ll> #define vi vector<int> #define pii pair<int, int> #define pll pair<ll, ll> #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define pb push_back #define p_b pop_back #define f first #define s second using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const ll sz = 505; char grid[sz+5][sz+5]; ll dist[sz+5][sz+5], n, m; bool ok(ll x, ll y) { if(x >= 1 && x <= n && y >= 1 && y <= m) return true; return false; } void bfs() { priority_queue<pair<pll, ll>>pq; pq.push({{1, 1}, 0}); while(!pq.empty()) { ll x = pq.top().f.f, y = pq.top().f.s, ch = pq.top().s; pq.pop(); dist[x][y] = ch; if(grid[x][y] != 'X'){ if(grid[x][y] == 'E') { ll nx = x, ny = y + 1; if(ok(nx, ny)) { if(ch < dist[nx][ny]){ pq.push({{nx, ny}, ch}); dist[nx][ny] = ch; } } nx = x, ny = y - 1; if(ok(nx, ny)) { if(ch + 2 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 2}); dist[nx][ny] = ch + 2; } } nx = x + 1, ny = y; if(ok(nx, ny)) { if(ch + 1 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 1}); dist[nx][ny] = ch + 1; } } nx = x - 1, ny = y; if(ok(nx, ny)) { if(ch + 3 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 3}); dist[nx][ny] = ch + 3; } } } else if(grid[x][y] == 'S') { ll nx = x, ny = y + 1; if(ok(nx, ny)) { if(ch + 3 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 3}); dist[nx][ny] = ch + 3; } } nx = x, ny = y - 1; if(ok(nx, ny)) { if(ch + 1 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 1}); dist[nx][ny] = ch + 1; } } nx = x + 1, ny = y; if(ok(nx, ny)) { if(ch < dist[nx][ny]){ pq.push({{nx, ny}, ch}); dist[nx][ny] = ch; } } nx = x - 1, ny = y; if(ok(nx, ny)) { if(ch + 2 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 2}); dist[nx][ny] = ch + 2; } } } else if(grid[x][y] == 'W') { ll nx = x, ny = y + 1; if(ok(nx, ny)) { if(ch + 2 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 2}); dist[nx][ny] = ch + 2; } } nx = x, ny = y - 1; if(ok(nx, ny)) { if(ch < dist[nx][ny]){ pq.push({{nx, ny}, ch}); dist[nx][ny] = ch; } } nx = x + 1, ny = y; if(ok(nx, ny)) { if(ch + 3 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 3}); dist[nx][ny] = ch + 3; } } nx = x - 1, ny = y; if(ok(nx, ny)) { if(ch + 1 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 1}); dist[nx][ny] = ch + 1; } } } else if(grid[x][y] == 'N') { ll nx = x, ny = y + 1; if(ok(nx, ny)) { if(ch + 1 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 1}); dist[nx][ny] = ch + 1; } } nx = x, ny = y - 1; if(ok(nx, ny)) { if(ch + 3 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 3}); dist[nx][ny] = ch + 3; } } nx = x + 1, ny = y; if(ok(nx, ny)) { if(ch + 2 < dist[nx][ny]){ pq.push({{nx, ny}, ch + 2}); dist[nx][ny] = ch + 2; } } nx = x - 1, ny = y; if(ok(nx, ny)) { if(ch < dist[nx][ny]){ pq.push({{nx, ny}, ch}); dist[nx][ny] = ch; } } } } } } void solve() { ll i, j; cin >> n >> m; for(i = 1; i <= n; i++) { string s; cin >> s; for(j = 1; j <= m; j++) grid[i][j] = s[j-1]; } for(i = 1; i <= n; i++) { for(j = 1; j <= m; j++) dist[i][j] = LLONG_MAX; } bfs(); cout << (dist[n][m] == LLONG_MAX ? -1 : dist[n][m]) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll tests = 1; //cin >> tests; while(tests--) { solve(); } }
#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...