Submission #1130622

#TimeUsernameProblemLanguageResultExecution timeMemory
1130622lopkusAwesome Arrowland Adventure (eJOI19_adventure)C++20
100 / 100
161 ms33976 KiB
#include <bits/stdc++.h> #define int long long #define mp make_pair #define pb push_back #define S second #define F first using namespace std; const int N = 250005; vector<pair<int,int>> adj[N]; vector<int> dist(N, 1e18); int n; int m; vector<int> mark(N); void dijkstra(int start){ priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minq; for(int i=0; i< n*m; i++){ dist[i]=1e9; } dist[0]=0; minq.push(mp(0, 0)); while(!minq.empty()){ pair<int, int> node=minq.top(); minq.pop(); if(mark[node.S]){ continue; } for(pair<int, int> to : adj[node.S]){ dist[to.F]=min(dist[to.F], node.F+to.S); minq.push(mp(dist[to.F], to.F)); } mark[node.S]=true; } } int cvor(int i, int j) { return i * n + j; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; char a[n][m]; for(int i=0; i< n; i++){ for(int j=0; j< m; j++){ cin >> a[i][j]; } } for(int i=0; i< n; i++){ for(int j=0; j< m; j++){ int node=i*m+j; if(a[i][j]=='X'){ continue; } else if(a[i][j]=='E'){ if(j<m-1)adj[node].pb(mp(node+1, 0)); if(j>0)adj[node].pb(mp(node-1, 2)); if(i>0)adj[node].pb(mp(node-m, 3)); if(i<n-1)adj[node].pb(mp(node+m, 1)); } else if(a[i][j]=='S'){ if(j<m-1)adj[node].pb(mp(node+1, 3)); if(j>0)adj[node].pb(mp(node-1, 1)); if(i>0)adj[node].pb(mp(node-m, 2)); if(i<n-1)adj[node].pb(mp(node+m, 0)); } else if(a[i][j]=='W'){ if(j<m-1)adj[node].pb(mp(node+1, 2)); if(j>0)adj[node].pb(mp(node-1, 0)); if(i>0)adj[node].pb(mp(node-m, 1)); if(i<n-1)adj[node].pb(mp(node+m, 3)); } else{ if(j<m-1)adj[node].pb(mp(node+1, 1)); if(j>0)adj[node].pb(mp(node-1, 3)); if(i>0)adj[node].pb(mp(node-m, 0)); if(i<n-1)adj[node].pb(mp(node+m, 2)); } } } dijkstra(0); if(dist[n*m-1]==1000000000){ cout << -1 << '\n'; return 0; } cout << dist[n*m-1] << '\n'; }
#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...