#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
int n, m, i, j;
char s[505][505];
vector<pair<pair<int, int>, int> > adj[505][505];
int d[505][505];
pair<int, int> curr;
set<pair<int, pair<int, int> > > S;
const int INF = INT_MAX/2;
bool isValid(int x, int y) {
return (x == n-1 && y == m-1) || (x >= 0 && x < n && y >= 0 && y < m && s[x][y] != 'X');
}
int main() {
scanf("%d%d", &n, &m);
for (i=0; i < n; ++i) scanf("\n%s", s[i]);
for (i=0; i < n; ++i) {
for (j=0; j < m; ++j) {
if (s[i][j] == 'X') continue;
else if (s[i][j] == 'W') {
if (isValid(i, j-1)) adj[i][j].push_back(make_pair(make_pair(i, j-1), 0));
if (isValid(i-1, j)) adj[i][j].push_back(make_pair(make_pair(i-1, j), 1));
if (isValid(i, j+1)) adj[i][j].push_back(make_pair(make_pair(i, j+1), 2));
if (isValid(i+1, j)) adj[i][j].push_back(make_pair(make_pair(i+1, j), 3));
}
else if (s[i][j] == 'N') {
if (isValid(i-1, j)) adj[i][j].push_back(make_pair(make_pair(i-1, j), 0));
if (isValid(i, j+1)) adj[i][j].push_back(make_pair(make_pair(i, j+1), 1));
if (isValid(i+1, j)) adj[i][j].push_back(make_pair(make_pair(i+1, j), 2));
if (isValid(i, j-1)) adj[i][j].push_back(make_pair(make_pair(i, j-1), 3));
}
else if (s[i][j] == 'E') {
if (isValid(i, j+1)) adj[i][j].push_back(make_pair(make_pair(i, j+1), 0));
if (isValid(i+1, j)) adj[i][j].push_back(make_pair(make_pair(i+1, j), 1));
if (isValid(i, j-1)) adj[i][j].push_back(make_pair(make_pair(i, j-1), 2));
if (isValid(i-1, j)) adj[i][j].push_back(make_pair(make_pair(i-1, j), 3));
}
else if (s[i][j] == 'S') {
if (isValid(i+1, j)) adj[i][j].push_back(make_pair(make_pair(i+1, j), 0));
if (isValid(i, j-1)) adj[i][j].push_back(make_pair(make_pair(i, j-1), 1));
if (isValid(i-1, j)) adj[i][j].push_back(make_pair(make_pair(i-1, j), 2));
if (isValid(i, j+1)) adj[i][j].push_back(make_pair(make_pair(i, j+1), 3));
}
}
}
for (i=0; i < n; ++i) for (j=0; j < m; ++j) d[i][j] = INF;
d[0][0] = 0;
S.insert(make_pair(0, make_pair(0,0)));
while (!S.empty()) {
curr = S.begin()->second;
S.erase(S.begin());
for (auto y : adj[curr.X][curr.Y]) {
if (d[y.X.X][y.X.Y] > d[curr.X][curr.Y]+y.Y) {
S.erase(make_pair(d[y.X.X][y.X.Y], y.X));
d[y.X.X][y.X.Y] = d[curr.X][curr.Y]+y.Y;
S.insert(make_pair(d[y.X.X][y.X.Y], y.X));
}
}
}
if (d[n-1][m-1] == INF) printf("-1\n");
else printf("%d\n", d[n-1][m-1]);
return 0;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
adventure.cpp:20:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | for (i=0; i < n; ++i) scanf("\n%s", s[i]);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6220 KB |
Output is correct |
3 |
Correct |
4 ms |
6220 KB |
Output is correct |
4 |
Correct |
4 ms |
6220 KB |
Output is correct |
5 |
Correct |
4 ms |
6296 KB |
Output is correct |
6 |
Correct |
4 ms |
6220 KB |
Output is correct |
7 |
Correct |
4 ms |
6220 KB |
Output is correct |
8 |
Correct |
4 ms |
6292 KB |
Output is correct |
9 |
Correct |
4 ms |
6292 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6220 KB |
Output is correct |
3 |
Correct |
4 ms |
6220 KB |
Output is correct |
4 |
Correct |
4 ms |
6220 KB |
Output is correct |
5 |
Correct |
4 ms |
6296 KB |
Output is correct |
6 |
Correct |
4 ms |
6220 KB |
Output is correct |
7 |
Correct |
4 ms |
6220 KB |
Output is correct |
8 |
Correct |
4 ms |
6292 KB |
Output is correct |
9 |
Correct |
4 ms |
6292 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
4 ms |
6300 KB |
Output is correct |
12 |
Correct |
4 ms |
6292 KB |
Output is correct |
13 |
Correct |
4 ms |
6300 KB |
Output is correct |
14 |
Correct |
4 ms |
6192 KB |
Output is correct |
15 |
Correct |
4 ms |
6220 KB |
Output is correct |
16 |
Correct |
4 ms |
6300 KB |
Output is correct |
17 |
Correct |
4 ms |
6220 KB |
Output is correct |
18 |
Correct |
4 ms |
6220 KB |
Output is correct |
19 |
Correct |
4 ms |
6220 KB |
Output is correct |
20 |
Correct |
4 ms |
6296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6220 KB |
Output is correct |
3 |
Correct |
4 ms |
6300 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6220 KB |
Output is correct |
3 |
Correct |
5 ms |
6220 KB |
Output is correct |
4 |
Correct |
4 ms |
6292 KB |
Output is correct |
5 |
Correct |
4 ms |
6348 KB |
Output is correct |
6 |
Correct |
4 ms |
6348 KB |
Output is correct |
7 |
Correct |
4 ms |
6296 KB |
Output is correct |
8 |
Correct |
4 ms |
6220 KB |
Output is correct |
9 |
Correct |
4 ms |
6264 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6220 KB |
Output is correct |
3 |
Correct |
4 ms |
6220 KB |
Output is correct |
4 |
Correct |
4 ms |
6220 KB |
Output is correct |
5 |
Correct |
4 ms |
6296 KB |
Output is correct |
6 |
Correct |
4 ms |
6220 KB |
Output is correct |
7 |
Correct |
4 ms |
6220 KB |
Output is correct |
8 |
Correct |
4 ms |
6292 KB |
Output is correct |
9 |
Correct |
4 ms |
6292 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
4 ms |
6300 KB |
Output is correct |
12 |
Correct |
4 ms |
6292 KB |
Output is correct |
13 |
Correct |
4 ms |
6300 KB |
Output is correct |
14 |
Correct |
4 ms |
6192 KB |
Output is correct |
15 |
Correct |
4 ms |
6220 KB |
Output is correct |
16 |
Correct |
4 ms |
6300 KB |
Output is correct |
17 |
Correct |
4 ms |
6220 KB |
Output is correct |
18 |
Correct |
4 ms |
6220 KB |
Output is correct |
19 |
Correct |
4 ms |
6220 KB |
Output is correct |
20 |
Correct |
4 ms |
6296 KB |
Output is correct |
21 |
Correct |
4 ms |
6220 KB |
Output is correct |
22 |
Correct |
4 ms |
6220 KB |
Output is correct |
23 |
Correct |
4 ms |
6300 KB |
Output is correct |
24 |
Correct |
5 ms |
6220 KB |
Output is correct |
25 |
Correct |
4 ms |
6220 KB |
Output is correct |
26 |
Correct |
4 ms |
6220 KB |
Output is correct |
27 |
Correct |
5 ms |
6220 KB |
Output is correct |
28 |
Correct |
4 ms |
6292 KB |
Output is correct |
29 |
Correct |
4 ms |
6348 KB |
Output is correct |
30 |
Correct |
4 ms |
6348 KB |
Output is correct |
31 |
Correct |
4 ms |
6296 KB |
Output is correct |
32 |
Correct |
4 ms |
6220 KB |
Output is correct |
33 |
Correct |
4 ms |
6264 KB |
Output is correct |
34 |
Correct |
4 ms |
6220 KB |
Output is correct |
35 |
Correct |
13 ms |
7500 KB |
Output is correct |
36 |
Correct |
4 ms |
6348 KB |
Output is correct |
37 |
Correct |
17 ms |
7788 KB |
Output is correct |
38 |
Correct |
11 ms |
8736 KB |
Output is correct |
39 |
Correct |
104 ms |
19108 KB |
Output is correct |
40 |
Correct |
103 ms |
19140 KB |
Output is correct |
41 |
Correct |
49 ms |
19000 KB |
Output is correct |
42 |
Correct |
117 ms |
19140 KB |
Output is correct |
43 |
Correct |
117 ms |
23332 KB |
Output is correct |
44 |
Correct |
44 ms |
19012 KB |
Output is correct |