This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
struct node {
int x, y, free, dist;
node() = default;
node(int x, int y, int free, int dist) : x(x), y(y), free(free), dist(dist) {}
};
std::vector<std::pair<int, int>> dir1 = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0}
};
std::vector<std::pair<int, int>> dir2 = {
{1, 1},
{1, -1},
{-1, 1},
{-1, -1}
};
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int r, c, n;
std::cin >> r >> c >> n;
int sx, sy, fx, fy;
std::cin >> sx >> sy >> fx >> fy;
--sx, --sy, --fx, --fy;
std::vector a(r, std::vector(c, '#'));
for (auto& i : a) {
for (auto& j : i) {
std::cin >> j;
}
}
std::deque<node> que;
std::vector used(r, std::vector(c, false));
que.emplace_back(sx, sy, 0, 0);
while (que.size()) {
node u = que.front();
assert(u.free >= 0);
que.pop_front();
if (u.x == fx && u.y == fy) {
std::cout << u.dist << '\n';
break;
}
if (used[u.x][u.y]) continue;
used[u.x][u.y] = true;
for (auto [dx, dy] : dir1) {
int i = u.x + dx, j = u.y + dy;
if (i < 0 || i >= r || j < 0 || j >= c) continue;
if (a[i][j] == '.') {
que.emplace_front(i, j, std::max(u.free - 1, 0), u.dist);
}
if (u.free == 0) {
que.emplace_back(i, j, n - 1, u.dist + 1);
} else {
que.emplace_front(i, j, u.free - 1, u.dist);
}
}
for (auto [dx, dy] : dir2) {
int i = u.x + dx, j = u.y + dy;
if (i < 0 || i >= r || j < 0 || j >= c) continue;
if (u.free >= 2) {
que.emplace_front(i, j, u.free - 1, u.dist);
} else if (n >= 2) {
que.emplace_back(i, j, n - 1, u.dist + 1);
}
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |