This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
char t;
vector<pair<int, int>> a[n*m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> t;
if(t == 'N'){
if(j < m-1) a[i*m+j].push_back({i*m+j+1, 1});
if(i < n-1) a[i*m+j].push_back({(i+1)*m+j, 2});
if(j > 0) a[i*m+j].push_back({i*m+j-1, 3});
if(i > 0) a[i*m+j].push_back({(i-1)*m+j, 0});
}
if(t == 'E'){
if(j < m-1) a[i*m+j].push_back({i*m+j+1, 0});
if(i < n-1) a[i*m+j].push_back({(i+1)*m+j, 1});
if(j > 0) a[i*m+j].push_back({i*m+j-1, 2});
if(i > 0) a[i*m+j].push_back({(i-1)*m+j, 3});
}
if(t == 'S'){
if(j < m-1) a[i*m+j].push_back({i*m+j+1, 3});
if(i < n-1) a[i*m+j].push_back({(i+1)*m+j, 0});
if(j > 0) a[i*m+j].push_back({i*m+j-1, 1});
if(i > 0) a[i*m+j].push_back({(i-1)*m+j, 2});
}
if(t == 'W'){
if(j < m-1) a[i*m+j].push_back({i*m+j+1, 2});
if(i < n-1) a[i*m+j].push_back({(i+1)*m+j, 3});
if(j > 0) a[i*m+j].push_back({i*m+j-1, 0});
if(i > 0) a[i*m+j].push_back({(i-1)*m+j, 1});
}
}
}
int d[n*m];
multiset<pair<int, int>> s;
for(int i = 1; i < n*m; i++){
d[i] = 1e9;
s.insert({1e9, i});
}
s.insert({0, 0});
d[0] = 0;
while(s.size() > 0){
pair<int, int> p = *s.begin();
int v = p.second;
s.erase(p);
for(int i = 0; i < a[v].size(); i++){
if(d[v] + a[v][i].second < d[a[v][i].first]){
s.erase({d[a[v][i].first], a[v][i].first});
d[a[v][i].first] = d[v] + a[v][i].second;
s.insert({d[a[v][i].first], a[v][i].first});
}
}
}
if(d[m*n-1] == 1e9) cout << -1;
else cout << d[m*n-1];
}
Compilation message (stderr)
adventure.cpp: In function 'int main()':
adventure.cpp:50:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i = 0; i < a[v].size(); i++){
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |