#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 5e3+5;
const int INF = 1e9;
char c[N][N];
int dis[N][N];
int main(){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
scanf(" %c", &c[i][j]), dis[i][j] = INF;
auto check = [&](int x, int y){
return (x >= 1 and x <= n and y >= 1 and y <= m);
};
using node = tuple<int, int, int>;
priority_queue<node, vector<node>, greater<node>> q;
q.push({0, 1, 1});
dis[1][1] = 0;
while(!q.empty()){
int x, y, step;
tie(step, x, y) = q.top();
q.pop();
if(c[x][y] == 'X'){
continue;
}
if(check(x, y+1)){
int add = 0;
if(c[x][y] == 'N')
add++;
else if(c[x][y] == 'W')
add += 2;
else if(c[x][y] == 'S')
add += 3;
if(umin(dis[x][1+y], dis[x][y]+add))
q.push({dis[x][1+y], x, 1+y});
}
if(check(x, y-1)){
int add = 0;
if(c[x][y] == 'S')
add++;
else if(c[x][y] == 'E')
add += 2;
else if(c[x][y] == 'N')
add += 3;
if(umin(dis[x][y-1], dis[x][y]+add))
q.push({dis[x][y-1], x, y-1});
}
if(check(x-1, y)){
int add = 0;
if(c[x][y] == 'W')
add++;
else if(c[x][y] == 'S')
add += 2;
else if(c[x][y] == 'E')
add += 3;
if(umin(dis[x-1][y], dis[x][y]+add))
q.push({dis[x-1][y], x-1, y});
}
if(check(x+1, y)){
int add = 0;
if(c[x][y] == 'E')
add++;
else if(c[x][y] == 'N')
add += 2;
else if(c[x][y] == 'W')
add += 3;
if(umin(dis[x+1][y], dis[x][y]+add))
q.push({dis[x+1][y], x+1, y});
}
}
int answer = dis[n][m];
if(answer == INF)
answer = -1;
printf("%d\n", answer);
return 0;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
adventure.cpp:25:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf(" %c", &c[i][j]), dis[i][j] = INF;
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
760 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
0 ms |
356 KB |
Output is correct |
7 |
Correct |
0 ms |
356 KB |
Output is correct |
8 |
Correct |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
352 KB |
Output is correct |
10 |
Correct |
0 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
760 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
360 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
356 KB |
Output is correct |
30 |
Correct |
0 ms |
356 KB |
Output is correct |
31 |
Correct |
0 ms |
356 KB |
Output is correct |
32 |
Correct |
0 ms |
452 KB |
Output is correct |
33 |
Correct |
0 ms |
352 KB |
Output is correct |
34 |
Correct |
0 ms |
356 KB |
Output is correct |
35 |
Correct |
4 ms |
976 KB |
Output is correct |
36 |
Correct |
0 ms |
356 KB |
Output is correct |
37 |
Correct |
5 ms |
1444 KB |
Output is correct |
38 |
Correct |
2 ms |
2640 KB |
Output is correct |
39 |
Correct |
43 ms |
5956 KB |
Output is correct |
40 |
Correct |
43 ms |
5732 KB |
Output is correct |
41 |
Correct |
13 ms |
5728 KB |
Output is correct |
42 |
Correct |
44 ms |
5936 KB |
Output is correct |
43 |
Correct |
66 ms |
8924 KB |
Output is correct |
44 |
Correct |
12 ms |
5736 KB |
Output is correct |