#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define F first
#define S second
const int N = 510;
const int INF = 1e9;
int n, m;
char a[N][N];
vector<pp> v[N * N];
set<pp> s; int dist[N * N];
int gd[N][N] = { 0 };
int lab(int x, int y){
return (x * m + y);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i){
for (int j = 0; j < m; ++j){
cin >> a[i][j];
if (a[i][j] != 'X' || (i == n - 1 && j == m - 1))
gd[i][j] = 1;
}
}
for (int i = 0; i < n; ++i){
for (int j = 0; j < m; ++j){
if (a[i][j] == 'X') continue;
vector<int> d(4);
if (a[i][j] == 'N')
d = {0, 1, 2, 3};
else if (a[i][j] == 'E')
d = {3, 0, 1, 2};
else if (a[i][j] == 'S')
d = {2, 3, 0, 1};
else d = {1, 2, 3, 0};
if (j + 1 < m && gd[i][j + 1])
v[lab(i, j)].push_back({d[1], lab(i, j + 1)});
if (j - 1 >= 0 && gd[i][j - 1])
v[lab(i, j)].push_back({d[3], lab(i, j - 1)});
if (i + 1 < n && gd[i + 1][j])
v[lab(i, j)].push_back({d[2], lab(i + 1, j)});
if (i - 1 >= 0 && gd[i - 1][j])
v[lab(i, j)].push_back({d[0], lab(i - 1, j)});
}
}
for (int i = 0; i < N * N; ++i)
dist[i] = INF;
dist[0] = 0; s.insert({0, 0});
int bio[N * N] = { 0 };
while (!s.empty()){
auto it = s.begin();
int x = (*it).S;
if (bio[x]){
s.erase(it); continue;
}
dist[x] = (*it).F;
if (x == lab(n - 1, m - 1)) break;
bio[x] = 1; s.erase(it);
for (pp y: v[x]){
if (dist[y.S] > dist[x] + y.F){
dist[y.S] = dist[x] + y.F;
s.insert({dist[y.S], y.S});
}
}
}
int val = dist[lab(n - 1, m - 1)];
if (val != INF)
cout << val;
else cout << -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8436 KB |
Output is correct |
2 |
Correct |
5 ms |
8488 KB |
Output is correct |
3 |
Correct |
5 ms |
8404 KB |
Output is correct |
4 |
Correct |
5 ms |
8496 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
5 ms |
8360 KB |
Output is correct |
7 |
Correct |
5 ms |
8404 KB |
Output is correct |
8 |
Correct |
5 ms |
8488 KB |
Output is correct |
9 |
Correct |
5 ms |
8484 KB |
Output is correct |
10 |
Correct |
5 ms |
8404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8436 KB |
Output is correct |
2 |
Correct |
5 ms |
8488 KB |
Output is correct |
3 |
Correct |
5 ms |
8404 KB |
Output is correct |
4 |
Correct |
5 ms |
8496 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
5 ms |
8360 KB |
Output is correct |
7 |
Correct |
5 ms |
8404 KB |
Output is correct |
8 |
Correct |
5 ms |
8488 KB |
Output is correct |
9 |
Correct |
5 ms |
8484 KB |
Output is correct |
10 |
Correct |
5 ms |
8404 KB |
Output is correct |
11 |
Correct |
4 ms |
8404 KB |
Output is correct |
12 |
Correct |
4 ms |
8404 KB |
Output is correct |
13 |
Correct |
5 ms |
8404 KB |
Output is correct |
14 |
Correct |
4 ms |
8404 KB |
Output is correct |
15 |
Correct |
4 ms |
8404 KB |
Output is correct |
16 |
Correct |
6 ms |
8488 KB |
Output is correct |
17 |
Correct |
4 ms |
8464 KB |
Output is correct |
18 |
Correct |
5 ms |
8404 KB |
Output is correct |
19 |
Correct |
5 ms |
8404 KB |
Output is correct |
20 |
Correct |
4 ms |
8500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8480 KB |
Output is correct |
2 |
Correct |
5 ms |
8404 KB |
Output is correct |
3 |
Correct |
4 ms |
8404 KB |
Output is correct |
4 |
Correct |
5 ms |
8368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8404 KB |
Output is correct |
2 |
Correct |
6 ms |
8368 KB |
Output is correct |
3 |
Correct |
5 ms |
8404 KB |
Output is correct |
4 |
Correct |
5 ms |
8404 KB |
Output is correct |
5 |
Correct |
5 ms |
8484 KB |
Output is correct |
6 |
Correct |
5 ms |
8404 KB |
Output is correct |
7 |
Correct |
4 ms |
8404 KB |
Output is correct |
8 |
Correct |
5 ms |
8404 KB |
Output is correct |
9 |
Correct |
5 ms |
8404 KB |
Output is correct |
10 |
Correct |
4 ms |
8360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8436 KB |
Output is correct |
2 |
Correct |
5 ms |
8488 KB |
Output is correct |
3 |
Correct |
5 ms |
8404 KB |
Output is correct |
4 |
Correct |
5 ms |
8496 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
5 ms |
8360 KB |
Output is correct |
7 |
Correct |
5 ms |
8404 KB |
Output is correct |
8 |
Correct |
5 ms |
8488 KB |
Output is correct |
9 |
Correct |
5 ms |
8484 KB |
Output is correct |
10 |
Correct |
5 ms |
8404 KB |
Output is correct |
11 |
Correct |
4 ms |
8404 KB |
Output is correct |
12 |
Correct |
4 ms |
8404 KB |
Output is correct |
13 |
Correct |
5 ms |
8404 KB |
Output is correct |
14 |
Correct |
4 ms |
8404 KB |
Output is correct |
15 |
Correct |
4 ms |
8404 KB |
Output is correct |
16 |
Correct |
6 ms |
8488 KB |
Output is correct |
17 |
Correct |
4 ms |
8464 KB |
Output is correct |
18 |
Correct |
5 ms |
8404 KB |
Output is correct |
19 |
Correct |
5 ms |
8404 KB |
Output is correct |
20 |
Correct |
4 ms |
8500 KB |
Output is correct |
21 |
Correct |
5 ms |
8480 KB |
Output is correct |
22 |
Correct |
5 ms |
8404 KB |
Output is correct |
23 |
Correct |
4 ms |
8404 KB |
Output is correct |
24 |
Correct |
5 ms |
8368 KB |
Output is correct |
25 |
Correct |
5 ms |
8404 KB |
Output is correct |
26 |
Correct |
6 ms |
8368 KB |
Output is correct |
27 |
Correct |
5 ms |
8404 KB |
Output is correct |
28 |
Correct |
5 ms |
8404 KB |
Output is correct |
29 |
Correct |
5 ms |
8484 KB |
Output is correct |
30 |
Correct |
5 ms |
8404 KB |
Output is correct |
31 |
Correct |
4 ms |
8404 KB |
Output is correct |
32 |
Correct |
5 ms |
8404 KB |
Output is correct |
33 |
Correct |
5 ms |
8404 KB |
Output is correct |
34 |
Correct |
4 ms |
8360 KB |
Output is correct |
35 |
Correct |
12 ms |
9456 KB |
Output is correct |
36 |
Correct |
5 ms |
8532 KB |
Output is correct |
37 |
Correct |
13 ms |
9708 KB |
Output is correct |
38 |
Correct |
11 ms |
10624 KB |
Output is correct |
39 |
Correct |
82 ms |
18812 KB |
Output is correct |
40 |
Correct |
80 ms |
18788 KB |
Output is correct |
41 |
Correct |
41 ms |
18656 KB |
Output is correct |
42 |
Correct |
82 ms |
18764 KB |
Output is correct |
43 |
Correct |
96 ms |
33204 KB |
Output is correct |
44 |
Correct |
40 ms |
18732 KB |
Output is correct |