#include <bits/stdc++.h>
// author : Pluton
#define OPT ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
#define ll long long
#define pb push_back
#define arr array
#define fi first
#define se second
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define all(a) a.begin(), a.end()
#define pii pair<int,int>
using namespace std;
const int INF = 1e18;
struct BIT {
int n;
vector<int> ft;
void init(int N) {
n = N + 5;
ft.assign(n + 5, 0);
}
void add(int pos, int val) {
for (pos = pos + 1; pos <= n; pos += pos & -pos) ft[pos] += val;
}
int get(int pos, int res = 0) {
for (pos = pos + 1; pos > 0; pos -= pos & -pos) res += ft[pos];
return res;
}
};
map<char, vector<vector<int>>> d;
int n, m;
bool f(int x, int y) {
if (x >= 0 && x < n && y >= 0 && y < m) {
return true;
}
return false;
}
const int N = 505;
vector<vector<char>> g;
vector<vector<int>> cost;
int bfs() {
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
q.push({0, 0, 0});
cost[0][0] = 0;
while (!q.empty()) {
vector<int> u = q.top();
q.pop();
auto move = d[g[u[2]][u[1]]];
for (auto e : move) {
int x = e[0] + u[1];
int y = e[1] + u[2];
if (f(x, y) && cost[y][x] > cost[u[2]][u[1]] + e[2]) {
cost[y][x] = cost[u[2]][u[1]] + e[2];
q.push({cost[y][x], x, y});
}
}
}
return cost[m - 1][n - 1];
}
void _() {
d['N'] = {{0, -1, 0}, {1, 0, 1}, {0, 1, 2}, {-1, 0, 3}};
d['E'] = {{0, -1, 3}, {1, 0, 0}, {0, 1, 1}, {-1, 0, 2}};
d['S'] = {{0, -1, 2}, {1, 0, 3}, {0, 1, 0}, {-1, 0, 1}};
d['W'] = {{0, -1, 1}, {1, 0, 2}, {0, 1, 3}, {-1, 0, 0}};
d['X'] = {};
cin >> m >> n;
for (int i = 0; i < m; ++i) {
g.push_back({});
cost.push_back({});
for (int j = 0; j < n; ++j) {
char c;
cin >> c;
g[i].push_back(c);
cost[i].push_back(INF);
}
}
int res = bfs();
cout << (res == INF ? -1 : res);
}
signed main() {
OPT
int tc = 1;
while (tc--) {
_();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
452 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
452 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 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 |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
9 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
12 ms |
744 KB |
Output is correct |
38 |
Correct |
1 ms |
1116 KB |
Output is correct |
39 |
Correct |
100 ms |
3124 KB |
Output is correct |
40 |
Correct |
107 ms |
3364 KB |
Output is correct |
41 |
Correct |
7 ms |
2908 KB |
Output is correct |
42 |
Correct |
107 ms |
2908 KB |
Output is correct |
43 |
Correct |
227 ms |
16364 KB |
Output is correct |
44 |
Correct |
4 ms |
2908 KB |
Output is correct |