/*
`-/oo+/- ``
.oyhhhhhhyo.`od
+hhhhyyoooos. h/
+hhyso++oosy- /s
.yoooossyyo:``-y`
..----.` ``.-/+:.`
`````..-::/.
`..```.-::///`
`-.....--::::/:
`.......--::////:
`...`....---:::://:
`......``..--:::::///:`
`---.......--:::::////+/`
----------::::::/::///++:
----:---:::::///////////:`
.----::::::////////////:-`
`----::::::::::/::::::::-
`.-----:::::::::::::::-
...----:::::::::/:-`
`.---::/+osss+:`
``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>
using namespace std;
const int INF = 2e9;
const int N = 500;
int n, m;
vector <pair <int, int>> g[5 + N * N];
bool viz[5 + N * N];
int dp[5 + N * N];
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> heap;
void Dijkstra(int nodStart) {
for(int i = 1; i <= n * m; i++) dp[i] = INF;
for(int i = 1; i <= n * m; i++) viz[i] = false;
dp[nodStart] = 0;
heap.push(make_pair(0, nodStart));
while(heap.size()) {
pair <int, int> top = heap.top();
heap.pop();
if(viz[top.second] == false) {
viz[top.second] = true;
for(auto to : g[top.second]) {
if(dp[to.second] > to.first + dp[top.second]) {
dp[to.second] = to.first + dp[top.second];
heap.push(make_pair(dp[to.second], to.second));
}
}
}
}
}
int main() {
#ifdef BLAT
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif // BLAT
cin >> n >> m;
for(int i = 1; i <= n; i++) {
string s;
cin >> s;
for(int j = 0; j < s.size(); j++) {
int idx = (i - 1) * m + j + 1;
int idxn, idxe, idxs, idxw;
idxn = idx - m;
idxs = idx + m;
idxe = idx + 1;
idxw = idx - 1;
if(s[j] == 'N') {
if(i > 1) g[idx].push_back(make_pair(0, idxn));
if(j < m - 1) g[idx].push_back(make_pair(1, idxe));
if(i < n) g[idx].push_back(make_pair(2, idxs));
if(j > 0) g[idx].push_back(make_pair(3, idxw));
} else if(s[j] == 'E') {
if(i > 1) g[idx].push_back(make_pair(3, idxn));
if(j < m - 1) g[idx].push_back(make_pair(0, idxe));
if(i < n) g[idx].push_back(make_pair(1, idxs));
if(j > 0) g[idx].push_back(make_pair(2, idxw));
} else if(s[j] == 'S') {
if(i > 1) g[idx].push_back(make_pair(2, idxn));
if(j < m - 1) g[idx].push_back(make_pair(3, idxe));
if(i < n) g[idx].push_back(make_pair(0, idxs));
if(j > 0) g[idx].push_back(make_pair(1, idxw));
} else if(s[j] == 'W') {
if(i > 1) g[idx].push_back(make_pair(1, idxn));
if(j < m - 1) g[idx].push_back(make_pair(2, idxe));
if(i < n) g[idx].push_back(make_pair(3, idxs));
if(j > 0) g[idx].push_back(make_pair(0, idxw));
}
}
}
Dijkstra(1);
if(dp[n * m] == INF) cout << -1 << '\n';
else cout << dp[n * m] << '\n';
return 0;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:85:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < s.size(); j++) {
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
8 ms |
6272 KB |
Output is correct |
4 |
Correct |
8 ms |
6144 KB |
Output is correct |
5 |
Correct |
8 ms |
6272 KB |
Output is correct |
6 |
Correct |
8 ms |
6144 KB |
Output is correct |
7 |
Correct |
9 ms |
6144 KB |
Output is correct |
8 |
Correct |
8 ms |
6272 KB |
Output is correct |
9 |
Correct |
9 ms |
6272 KB |
Output is correct |
10 |
Correct |
8 ms |
6144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
8 ms |
6272 KB |
Output is correct |
4 |
Correct |
8 ms |
6144 KB |
Output is correct |
5 |
Correct |
8 ms |
6272 KB |
Output is correct |
6 |
Correct |
8 ms |
6144 KB |
Output is correct |
7 |
Correct |
9 ms |
6144 KB |
Output is correct |
8 |
Correct |
8 ms |
6272 KB |
Output is correct |
9 |
Correct |
9 ms |
6272 KB |
Output is correct |
10 |
Correct |
8 ms |
6144 KB |
Output is correct |
11 |
Correct |
8 ms |
6144 KB |
Output is correct |
12 |
Correct |
8 ms |
6272 KB |
Output is correct |
13 |
Correct |
8 ms |
6272 KB |
Output is correct |
14 |
Correct |
8 ms |
6144 KB |
Output is correct |
15 |
Correct |
8 ms |
6144 KB |
Output is correct |
16 |
Correct |
8 ms |
6272 KB |
Output is correct |
17 |
Correct |
8 ms |
6272 KB |
Output is correct |
18 |
Correct |
8 ms |
6272 KB |
Output is correct |
19 |
Correct |
8 ms |
6272 KB |
Output is correct |
20 |
Correct |
8 ms |
6272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6144 KB |
Output is correct |
3 |
Correct |
8 ms |
6144 KB |
Output is correct |
4 |
Correct |
9 ms |
6272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
8 ms |
6272 KB |
Output is correct |
4 |
Correct |
8 ms |
6144 KB |
Output is correct |
5 |
Correct |
8 ms |
6272 KB |
Output is correct |
6 |
Correct |
8 ms |
6272 KB |
Output is correct |
7 |
Correct |
8 ms |
6272 KB |
Output is correct |
8 |
Correct |
10 ms |
6144 KB |
Output is correct |
9 |
Correct |
8 ms |
6272 KB |
Output is correct |
10 |
Correct |
8 ms |
6144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
8 ms |
6272 KB |
Output is correct |
4 |
Correct |
8 ms |
6144 KB |
Output is correct |
5 |
Correct |
8 ms |
6272 KB |
Output is correct |
6 |
Correct |
8 ms |
6144 KB |
Output is correct |
7 |
Correct |
9 ms |
6144 KB |
Output is correct |
8 |
Correct |
8 ms |
6272 KB |
Output is correct |
9 |
Correct |
9 ms |
6272 KB |
Output is correct |
10 |
Correct |
8 ms |
6144 KB |
Output is correct |
11 |
Correct |
8 ms |
6144 KB |
Output is correct |
12 |
Correct |
8 ms |
6272 KB |
Output is correct |
13 |
Correct |
8 ms |
6272 KB |
Output is correct |
14 |
Correct |
8 ms |
6144 KB |
Output is correct |
15 |
Correct |
8 ms |
6144 KB |
Output is correct |
16 |
Correct |
8 ms |
6272 KB |
Output is correct |
17 |
Correct |
8 ms |
6272 KB |
Output is correct |
18 |
Correct |
8 ms |
6272 KB |
Output is correct |
19 |
Correct |
8 ms |
6272 KB |
Output is correct |
20 |
Correct |
8 ms |
6272 KB |
Output is correct |
21 |
Correct |
9 ms |
6272 KB |
Output is correct |
22 |
Correct |
8 ms |
6144 KB |
Output is correct |
23 |
Correct |
8 ms |
6144 KB |
Output is correct |
24 |
Correct |
9 ms |
6272 KB |
Output is correct |
25 |
Correct |
8 ms |
6272 KB |
Output is correct |
26 |
Correct |
8 ms |
6272 KB |
Output is correct |
27 |
Correct |
8 ms |
6272 KB |
Output is correct |
28 |
Correct |
8 ms |
6144 KB |
Output is correct |
29 |
Correct |
8 ms |
6272 KB |
Output is correct |
30 |
Correct |
8 ms |
6272 KB |
Output is correct |
31 |
Correct |
8 ms |
6272 KB |
Output is correct |
32 |
Correct |
10 ms |
6144 KB |
Output is correct |
33 |
Correct |
8 ms |
6272 KB |
Output is correct |
34 |
Correct |
8 ms |
6144 KB |
Output is correct |
35 |
Correct |
16 ms |
7168 KB |
Output is correct |
36 |
Correct |
8 ms |
6328 KB |
Output is correct |
37 |
Correct |
20 ms |
7424 KB |
Output is correct |
38 |
Correct |
16 ms |
7936 KB |
Output is correct |
39 |
Correct |
105 ms |
17144 KB |
Output is correct |
40 |
Correct |
99 ms |
17016 KB |
Output is correct |
41 |
Correct |
54 ms |
17016 KB |
Output is correct |
42 |
Correct |
107 ms |
17160 KB |
Output is correct |
43 |
Correct |
124 ms |
21492 KB |
Output is correct |
44 |
Correct |
56 ms |
17148 KB |
Output is correct |