Submission #845862

#TimeUsernameProblemLanguageResultExecution timeMemory
845862konberAwesome Arrowland Adventure (eJOI19_adventure)C++14
100 / 100
107 ms27304 KiB
#include <iostream> #include <vector> #include <cstring> #include <queue> using namespace std; typedef pair<int,int> pi; vector<pair<int, pi>> g[502][502]; int N, M; int dijkstra(){ int d[M][N], z[M][N]; for(int i=0; i < M; i++) for(int j=0; j < N; j++){ d[i][j] = 1e9; z[i][j] = 0; } d[0][0] = 0; priority_queue<pair<int, pi>> pq; pq.push(make_pair(0, make_pair(0, 0))); while(!pq.empty()){ int i=pq.top().second.first; int j=pq.top().second.second; pq.pop(); if(z[i][j]) continue; z[i][j] = 1; for(auto u: g[i][j]){ if(d[u.second.first][u.second.second] > d[i][j] + u.first){ d[u.second.first][u.second.second] = d[i][j] + u.first; pq.push(make_pair(-d[u.second.first][u.second.second], make_pair(u.second.first, u.second.second))); } } } return d[M-1][N-1]; } int main() { scanf("%d%d", &M, &N); for(int i=0; i < M; i++){ for(int j=0; j < N; j++){ char o; scanf(" %c", &o); if(o=='E'){ if(j < N-1) g[i][j].push_back(make_pair(0, make_pair(i, j+1))); if(i < M-1) g[i][j].push_back(make_pair(1, make_pair(i+1, j))); if(j) g[i][j].push_back(make_pair(2, make_pair(i, j-1))); if(i) g[i][j].push_back(make_pair(3, make_pair(i-1, j))); } else if(o=='N'){ if(j < N-1) g[i][j].push_back(make_pair(1, make_pair(i, j+1))); if(i < M-1) g[i][j].push_back(make_pair(2, make_pair(i+1, j))); if(j) g[i][j].push_back(make_pair(3, make_pair(i, j-1))); if(i) g[i][j].push_back(make_pair(0, make_pair(i-1, j))); } else if(o=='W'){ if(j < N-1) g[i][j].push_back(make_pair(2, make_pair(i, j+1))); if(i < M-1) g[i][j].push_back(make_pair(3, make_pair(i+1, j))); if(j) g[i][j].push_back(make_pair(0, make_pair(i, j-1))); if(i) g[i][j].push_back(make_pair(1, make_pair(i-1, j))); } else if(o=='S'){ if(j < N-1) g[i][j].push_back(make_pair(3, make_pair(i, j+1))); if(i < M-1) g[i][j].push_back(make_pair(0, make_pair(i+1, j))); if(j) g[i][j].push_back(make_pair(1, make_pair(i, j-1))); if(i) g[i][j].push_back(make_pair(2, make_pair(i-1, j))); } } } int ans=dijkstra(); if(ans==1e9) printf("-1\n"); else printf("%d\n", ans); }

Compilation message (stderr)

adventure.cpp: In function 'int main()':
adventure.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |     scanf("%d%d", &M, &N);
      |     ~~~~~^~~~~~~~~~~~~~~~
adventure.cpp:45:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |             scanf(" %c", &o);
      |             ~~~~~^~~~~~~~~~~
#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...