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;
int n, m, x;
int sr, sc, gr, gc;
vector <vector <char>> a;
priority_queue <array <int, 3>, vector <array <int, 3>>, greater <>> pq;
vector <vector <int>> dist;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
void solve () {
cin >> n >> m >> x >> sr >> sc >> gr >> gc;
a.resize(n);
dist.resize(n);
for (auto &i : dist) {
i.resize(m);
for (auto &j : i) {
j = 1e9;
}
}
for (auto &i : a) {
i.resize(m);
for (auto &j : i) {
cin >> j;
}
}
sr--; sc--; gr--; gc--;
dist[sr][sc] = 0;
pq.push({0, sr, sc});
while (!pq.empty()) {
auto k = pq.top();
pq.pop();
if (k[0] > dist[k[1]][k[2]]) {
continue;
}
for (int j = 0; j < n; j++) {
for (int l = 0; l < m; l++) {
if (abs(j - k[1]) <= x && abs(l - k[2]) <= x) {
if (k[0] + 1 < dist[j][l]) {
dist[j][l] = k[0] + 1;
pq.push({dist[j][l], j, l});
}
}
}
}
for (int i = 0; i < 4; i++) {
int x = k[1] + dx[i], y = k[2] + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m) {
if (a[x][y] != '#') {
if (dist[x][y] > k[0]) {
dist[x][y] = k[0];
pq.push({dist[x][y], x, y});
}
}
}
}
}
cout << dist[gr][gc] << '\n';
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
# | 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... |