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>
using namespace std;
#define int long long
using pii = pair<int, int>;
#define IO ios::sync_with_stdio(0), cin.tie(0)
#define FOR(p, a, b) for (int p = a; p <= b; p++)
void dbg() {;}
template<class T, class ...U>
void dbg(T a, U ...b) {cout << a << (sizeof...(b) ? ", " : " "); dbg(b...);}
void ent() {cout << "\n";}
/// ---- INITIAL END ----
const int INF = 1e15;
const int N = 200005;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
signed main() {
IO;
// freopen("out.txt", "w", stdout);
int r, c, n;
cin >> r >> c >> n;
int sx, sy, tx, ty;
cin >> sx >> sy >> tx >> ty;
sx -= 1; sy -= 1; tx -= 1; ty -= 1;
vector<string> v(r);
FOR (i, 0, r - 1) cin >> v[i];
auto inside = [&](int x, int y) {
return (0 <= x && x < r && 0 <= y && y < c);
};
vector<vector<int>> dis(r, vector<int>(c, INF));
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> Q;
Q.push({0, {sx, sy}});
dis[sx][sy] = 0;
int ans = INF;
while (! Q.empty()) {
int step = Q.top().first;
pii pos = Q.top().second;
auto [x, y] = pos;
Q.pop();
if (ans != INF && step >= ans) {
cout << ans << "\n";
return 0;
}
if (step != dis[x][y]) continue;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (inside(nx, ny) && v[nx][ny] == '.') {
if (step < dis[nx][ny]) {
if (nx == tx && ny == ty) {
cout << step << "\n";
return 0;
}
dis[nx][ny] = step;
Q.push({dis[nx][ny], {nx, ny}});
}
}
}
if (x - n <= tx && tx <= x + n && y - n <= ty && ty <= y + n) {
ans = min(ans, step + 1);
continue;
}
for (int nx = max(0ll, x - n); nx <= min(r - 1, x + n); nx++) {
for (int ny = max(0ll, y - n); ny <= min(c - 1, y + n); ny++) {
// cout << nx << " " << ny; ent();
if (abs(nx - x) + abs(ny - y) == 2 * n) continue;
if (step + 1 < dis[nx][ny]) {
dis[nx][ny] = step + 1;
Q.push({dis[nx][ny], {nx, ny}});
}
}
}
}
cout << ans << "\n";
return 0;
}
# | 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... |