#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 dp[10001][1001] = {};
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] = {};
int cost[502][502] = {};
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(100 * 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];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
1 ms |
2900 KB |
Output is correct |
14 |
Correct |
1 ms |
2516 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2516 KB |
Output is correct |
2 |
Correct |
1 ms |
2516 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
4948 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
1 ms |
2900 KB |
Output is correct |
14 |
Correct |
1 ms |
2516 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
2516 KB |
Output is correct |
23 |
Correct |
1 ms |
2516 KB |
Output is correct |
24 |
Correct |
1 ms |
2516 KB |
Output is correct |
25 |
Runtime error |
6 ms |
4948 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |