#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
const int sz = 1e6 + 1, mod = 1e9 + 7, inf = -1e18;
int change(char a, char b) {
if (a == 'N') {
int ans = 0;
if (b == 'N')return ans;
ans++;
if (b == 'E')return ans;
ans++;
if (b == 'S')return ans;
ans++;
if (b == 'W')return ans;
return ans;
}
if (a == 'W') {
int ans = 0;
if (b == 'W')return ans;
ans++;
if (b == 'N')return ans;
ans++;
if (b == 'E')return ans;
ans++;
if (b == 'S')return ans;
return ans;
}
if (a == 'S') {
int ans = 0;
if (b == 'S')return ans;
ans++;
if (b == 'W')return ans;
ans++;
if (b == 'N')return ans;
ans++;
if (b == 'E')return ans;
return ans;
}
if (a == 'E') {
int ans = 0;
if (b == 'E')return ans;
ans++;
if (b == 'S')return ans;
ans++;
if (b == 'W')return ans;
ans++;
if (b == 'N')return ans;
return ans;
}
return 0;
}
signed main()
{
int n, m;
cin >> n >> m;
char a[502][502] = {};
map<pair<int,int>, int>cost;
vector<vector<int>>vc(n * m + 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] != 'X' && j > 1) {
vc[m * (i - 1) + j].push_back(m * (i - 1) + j - 1);
cost[{m* (i - 1) + j, m* (i - 1) + j - 1}] = change(a[i][j], 'W');
}
if (a[i][j] != 'X' && j < m) {
vc[m * (i - 1) + j].push_back(m * (i - 1) + j + 1);
cost[{ m * (i - 1) + j,m * (i - 1) + j + 1 }] = change(a[i][j], 'E');
}
if (a[i][j] != 'X' && i > 1) {
vc[m * (i - 1) + j].push_back(m * (i - 2) + j);
cost[{m* (i - 1) + j,m * (i - 2) + j}] = change(a[i][j], 'N');
}
if (a[i][j] != 'X' && i < n) {
vc[m * (i - 1) + j].push_back(m * (i)+j);
cost[{m* (i - 1) + j, m* (i)+j}] = change(a[i][j], 'S');
}
}
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;
vector<int>ans(n * m + 1, 1e18);
ans[1] = 0;
pq.push({ 0,1 });
while (!pq.empty()) {
int u = pq.top().second;
int cos = pq.top().first;
//cout << u << ' ' << cos << endl;
pq.pop();
if (ans[u] < cos) {
continue;
}
for (auto i : vc[u]) {
if (cos + cost[{u, i}] < ans[i]) {
ans[i] = cos + cost[{u, i}];
pq.push({ ans[i],i });
}
}
}
if (ans[n * m] == 1e18)ans[n * m] = -1;
cout << ans[n * m];
//cout << ans[m * (n - 1) + m];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
564 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
564 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
564 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
25 ms |
6588 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
30 ms |
7636 KB |
Output is correct |
38 |
Correct |
25 ms |
11532 KB |
Output is correct |
39 |
Correct |
325 ms |
67896 KB |
Output is correct |
40 |
Correct |
361 ms |
67836 KB |
Output is correct |
41 |
Correct |
154 ms |
67620 KB |
Output is correct |
42 |
Correct |
316 ms |
67828 KB |
Output is correct |
43 |
Correct |
339 ms |
86488 KB |
Output is correct |
44 |
Correct |
157 ms |
67724 KB |
Output is correct |