Submission #658729

#TimeUsernameProblemLanguageResultExecution timeMemory
658729BananAwesome Arrowland Adventure (eJOI19_adventure)C++17
100 / 100
86 ms9020 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long #define double long double #define endl '\n' #define sz(a) (int)a.size() #define pb push_back #define fs first #define sc second #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() int const INF = LONG_LONG_MAX; int n, m; string s[505]; vector<vector<int>> dist(505, vector<int>(505, INF)); priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq; int dirx[4]={-1, 0, 1, 0}, diry[4]={0, 1, 0, -1}; bool cost(char c, int* arr) { if(c=='X'){return 1;} else if(c=='N') { arr[0]=0;arr[1]=1;arr[2]=2;arr[3]=3; } else if(c=='E') { arr[0]=3;arr[1]=0;arr[2]=1;arr[3]=2; } else if(c=='S') { arr[0]=2;arr[1]=3;arr[2]=0;arr[3]=1; } else if(c=='W') { arr[0]=1;arr[1]=2;arr[2]=3;arr[3]=0; } return 0; } void dikj() { while(!pq.empty()) { pair<int, pair<int, int>> p; p=pq.top(); pq.pop(); int d=p.fs, cx=p.sc.fs, cy=p.sc.sc; if(d!=dist[cx][cy]){continue;} int val[4]; if(cost(s[cx][cy], val)){continue;} for(int i=0;i<4;i++) { int tox=cx+dirx[i], toy=cy+diry[i]; if(tox>=0 && tox<n && toy>=0 && toy<m) { if(dist[tox][toy]>d+val[i]) { dist[tox][toy]=d+val[i]; pq.push({dist[tox][toy], {tox, toy}}); } } } } } void solve() { cin>>n>>m; for(int i=0;i<n;i++) { cin>>s[i]; } dist[0][0]=0; pq.push({dist[0][0], {0, 0}}); dikj(); if(dist[n-1][m-1]==INF){cout<<-1;}else{cout<<dist[n-1][m-1];} } int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int tc=1; //cin>>tc; while(tc--) { solve(); } 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...