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;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
int dist[1010][1010];
bool vis[1010][1010], block[1010][1010];
const int dr[4] = {-1, 0, 0, 1};
const int dc[4] = {0, -1, 1, 0};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, k; cin >> n >> m >> k;
int stx, sty, enx, eny; cin >> stx >> sty >> enx >> eny;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c; cin >> c;
if (c == '#') block[i][j] = 1;
dist[i][j] = INT_MAX;
}
}
function<bool(int,int)> ok = [&] (int i, int j) {
if (i < 1 || j < 1) return 0;
if (i > n || j > m) return 0;
return 1;
};
function<int(int,int,int,int)> distance = [&] (int i, int j, int u, int v) {
return max(abs(i - u), abs(j - v));
};
dist[stx][sty] = 0;
priority_queue<tt> pq;
pq.emplace(0, stx, sty);
while (pq.size()) {
int d, a, b; tie(d, a, b) = pq.top(); pq.pop();
if (vis[a][b]) continue;
vis[a][b] = 1;
//cout << "visting " << a << " " << b << " " << dist[a][b] << "\n";
function<void(int,int,int)> transition = [&] (int i, int j, int w) {
if (dist[a][b] + w < dist[i][j]) {
dist[i][j] = dist[a][b] + w;
pq.emplace(-dist[i][j], i, j);
}
};
if (block[a][b]) {
if (distance(a, b, enx, eny) < k)
return cout << dist[a][b], 0;
int xL = max(1, a - k), xR = min(n, a + k);
for (int j = max(1, b - k + 1); j <= min(m, b + k - 1); j++)
transition(xL, j, block[xL][j]), transition(xR, j, block[xR][j]);
int yL = max(1, b - k), yR = min(m, b + k);
for (int i = max(1, a - k + 1); i <= min(n, a + k - 1); i++)
transition(i, yL, block[i][yL]), transition(i, yR, block[i][yR]);
}
else {
if (distance(a, b, enx, eny) == 0)
return cout << dist[a][b], 0;
for (int k = 0; k < 4; k++)
if (ok(a + dr[k], b + dc[k])) transition(a + dr[k], b + dc[k], block[a + dr[k]][b + dc[k]]);
}
}
cout << -1;
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... |