#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
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++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
276 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
276 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
224 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
276 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
224 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
300 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
300 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
18 ms |
2772 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
17 ms |
3316 KB |
Output is correct |
38 |
Correct |
13 ms |
4820 KB |
Output is correct |
39 |
Correct |
199 ms |
28408 KB |
Output is correct |
40 |
Correct |
192 ms |
28496 KB |
Output is correct |
41 |
Correct |
110 ms |
28496 KB |
Output is correct |
42 |
Correct |
196 ms |
28516 KB |
Output is correct |
43 |
Correct |
169 ms |
30728 KB |
Output is correct |
44 |
Correct |
96 ms |
28492 KB |
Output is correct |