#include <bits/stdc++.h>
using std::array;
using std::pair;
using std::tuple;
using std::vector;
constexpr int MAX = 500;
constexpr int INF = std::numeric_limits<int>::max() / 2;
constexpr int DI[] = {0, 1, 0, -1};
constexpr int DJ[] = {1, 0, -1, 0};
char grid[MAX + 2][MAX + 2];
int dist[MAX + 2][MAX + 2][9][9];
int state[MAX + 2][MAX + 2][4];
pair<int, int> stops[MAX + 2][MAX + 2][4];
pair<int, int> dfs(const int i, const int j, const int k) {
if (state[i][j][k] == 2) {
return stops[i][j][k];
}
if (state[i][j][k] == 1) {
return stops[i][j][k] = {i, j};
}
state[i][j][k] = 1;
int nk = k;
if (grid[i][j] == 'A') {
nk = (k + 3) % 4;
}
if (grid[i][j] == 'C') {
nk = (k + 1) % 4;
}
const int ni = i + DI[nk], nj = j + DJ[nk];
if (grid[ni][nj] == 'x') {
state[i][j][k] = 2;
return stops[i][j][k] = {i, j};
} else {
stops[i][j][k] = dfs(ni, nj, nk);
if (state[ni][nj][nk] == 1) {
stops[i][j][k] = {i, j};
} else {
state[i][j][k] = 2;
}
return stops[i][j][k];
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, H, W;
std::cin >> N >> W >> H;
H += 2, W += 2;
for (int i = 0; i < W; ++i) {
grid[0][i] = grid[H - 1][i] = 'x';
}
for (int i = 0; i < H; ++i) {
grid[i][0] = grid[i][W - 1] = 'x';
}
for (int i = 1; i < H - 1; ++i) {
for (int j = 1; j < W - 1; ++j) {
std::cin >> grid[i][j];
}
}
std::queue<array<int, 4>> que;
const auto push = [&](const int i, const int j, const int k, const int l, const int d) {
if (dist[i][j][k][l] > d) {
dist[i][j][k][l] = d;
que.push({i, j, k, l});
}
};
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
for (int k = 0; k < N; ++k) {
for (int l = 0; l < N; ++l) {
dist[i][j][k][l] = INF;
}
}
if ('1' <= grid[i][j] and grid[i][j] <= '9') {
const int c = grid[i][j] - '1';
push(i, j, c, c, 0);
}
}
}
while (!que.empty()) {
const auto [i, j, k, l] = que.front();
que.pop();
if (k > 0) {
push(i, j, k - 1, l, dist[i][j][k][l] + dist[i][j][k - 1][k - 1]);
}
if (l + 1 < N) {
push(i, j, k, l + 1, dist[i][j][k][l] + dist[i][j][l + 1][l + 1]);
}
for (int d = 0; d < 4; ++d) {
const auto [ni, nj] = dfs(i, j, d);
assert(state[i][j][d] == 2);
push(ni, nj, k, l, dist[i][j][k][l] + 1);
}
}
int ans = INF;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
ans = std::min(ans, dist[i][j][0][N - 1]);
}
}
std::cout << (ans == INF ? -1 : ans) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Incorrect |
101 ms |
35532 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Incorrect |
101 ms |
35532 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |