Submission #277556

#TimeUsernameProblemLanguageResultExecution timeMemory
277556JuliusMieliauskasAwesome Arrowland Adventure (eJOI19_adventure)C++14
100 / 100
132 ms22000 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define vi vector<int> #define vll vector<long long> #define MOD 1000000007 #define endl '\n' typedef long long ll; void print(vi v){ cout<<"Contents of vector:\n"; for(auto x : v) cout<<x<<" "; cout<<endl<<endl; } int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; vector<pair<int, int> > adj[509 * 509]; int m, n; char ar[509][509]; int dists[509 * 509]; bool in(int i, int j){ return (i >= 0) && (j >= 0) && (i<m) && (j<n); } void fillAdj(){ for(int i = 0; i<m; i++){ for(int j = 0; j<n; j++){ if(ar[i][j] == 'E'){ for(int k = 0; k<4; k++){ int ci = i + dy[k], cj = j + dx[k]; if(in(ci, cj)){ if(k == 0) { /// Going W adj[i*n + j].push_back({ci*n + cj, 2}); } if(k == 1) { /// Going N adj[i*n + j].push_back({ci*n + cj, 3}); } if(k == 2) { /// Going E adj[i*n + j].push_back({ci*n + cj, 0}); } if(k == 3) { /// Going S adj[i*n + j].push_back({ci*n + cj, 1}); } } } } if(ar[i][j] == 'W'){ for(int k = 0; k<4; k++){ int ci = i + dy[k], cj = j + dx[k]; if(in(ci, cj)){ if(k == 0) { /// Going W adj[i*n + j].push_back({ci*n + cj, 0}); } if(k == 1) { /// Going N adj[i*n + j].push_back({ci*n + cj, 1}); } if(k == 2) { /// Going E adj[i*n + j].push_back({ci*n + cj, 2}); } if(k == 3) { /// Going S adj[i*n + j].push_back({ci*n + cj, 3}); } } } } if(ar[i][j] == 'S'){ for(int k = 0; k<4; k++){ int ci = i + dy[k], cj = j + dx[k]; if(in(ci, cj)){ if(k == 0) { /// Going W adj[i*n + j].push_back({ci*n + cj, 1}); } if(k == 1) { /// Going N adj[i*n + j].push_back({ci*n + cj, 2}); } if(k == 2) { /// Going E adj[i*n + j].push_back({ci*n + cj, 3}); } if(k == 3) { /// Going S adj[i*n + j].push_back({ci*n + cj, 0}); } } } } if(ar[i][j] == 'N'){ for(int k = 0; k<4; k++){ int ci = i + dy[k], cj = j + dx[k]; if(in(ci, cj)){ if(k == 0) { /// Going W adj[i*n + j].push_back({ci*n + cj, 3}); } if(k == 1) { /// Going N adj[i*n + j].push_back({ci*n + cj, 0}); } if(k == 2) { /// Going E adj[i*n + j].push_back({ci*n + cj, 1}); } if(k == 3) { /// Going S adj[i*n + j].push_back({ci*n + cj, 2}); } } } } } } } void dijkstra(int source){ bool visited[n*m]; for(int i = 0; i<m; i++){ for(int j = 0; j<n; j++) dists[i*n + j] = INT_MAX; } for(int i = 0; i<m; i++) for(int j = 0; j<n; j++) visited[i*n + j] = false; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; pq.push({0, source}); dists[source] = 0; while (!pq.empty()) { int u = pq.top().second; pq.pop(); if (visited[u]) continue; else visited[u] = true; //cout<<"From "<<u<<":"<<endl; for(auto v : adj[u]) { //cout<<v.first<<endl; if (dists[v.first] > dists[u] + v.second) { dists[v.first] = dists[u] + v.second; pq.push({dists[v.first], v.first}); } } //cout<<endl; } } void solve(){ cin>>m>>n; for(int i = 0; i<m; i++) for(int j = 0; j<n; j++) cin>>ar[i][j]; fillAdj(); dijkstra(0); /*for(int i = 0; i<m; i++){ for(int j = 0; j<n; j++){ cout<<dists[i*n + j]<<" "; } cout<<endl; }*/ if(dists[m*n - 1] == INT_MAX) cout<<"-1"<<endl; else cout<<dists[m*n - 1]<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //ifstream cin(""); ofstream cout("");///cia failai //int T; cin>>T; int T = 1; for(int it = 1; it<=T; it++){ 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...