#include <bits/stdc++.h>
using namespace std;
#define dbg(x) x
#define prt(x) dbg(cerr << x)
#define pv(x) dbg(cerr << #x << " = " << x << '\n')
#define pv2(x) dbg(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define parr(x) dbg(prt(#x << " = { "); for (auto y : x) prt(y << ' '); prt("}\n");)
#define parr2(x) dbg(prt(#x << " = { "); for (auto [y, z] : x) prt(y << ',' << z << " "); prt("}\n");)
#define parr2d(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr(arr);} prt('\n'));
#define parr2d2(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr2(arr);} prt('\n'));
/*
*/
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
const int inf = 2e9 + 5;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
int i0, j0, i1, j1;
cin >> i0 >> j0 >> i1 >> j1;
i0--; j0--; i1--; j1--;
vector<string> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
function<bool(int, int)> ok = [&] (int i, int j) {
return i >= 0 && i < n && j >= 0 && j < m;
};
if (k == 1) {
vector<vector<int>> dist(n, vector<int>(m, inf));
deque<array<int, 3>> q;
q.push_back({i0, j0, 0});
dist[i0][j0] = 0;
while (q.size()) {
int i = q[0][0], j = q[0][1], _d = q[0][2];
q.pop_front();
if (dist[i][j] != _d) continue;
for (int d = 0; d < 4; d++) {
int ni = i + dx[d], nj = j + dy[d];
if (!ok(ni, nj)) continue;
if (a[ni][nj] == '.') {
if (dist[i][j] < dist[ni][nj]) {
dist[ni][nj] = dist[i][j];
q.push_front({ni, nj, dist[i][j]});
}
} else {
if (dist[i][j] + 1 < dist[ni][nj]) {
dist[ni][nj] = dist[i][j] + 1;
q.push_back({ni, nj, dist[i][j] + 1});
}
}
}
}
cout << dist[i1][j1] << '\n';
} else if (n * m <= 1000) {
vector<vector<int>> dist(n, vector<int>(m, inf));
dist[i0][j0] = 0;
vector<vector<bool>> vis(n, vector<bool>(m, false));
queue<array<int, 2>> q;
q.push({i0, j0});
vis[i0][j0] = true;
while (q.size()) {
int i = q.front()[0], j = q.front()[1];
q.pop();
for (int d = 0; d < 4; d++) {
int ni = i + dx[d], nj = j + dy[d];
if (!ok(ni, nj)) continue;
if (a[ni][nj] == '.' && dist[ni][nj] > 0) {
dist[ni][nj] = 0;
vis[i][j] = true;
q.push({ni, nj});
}
}
}
for (int it = 0; it < n * m; it++) {
int best = inf, bi = -1, bj = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (dist[i][j] < best) {
best = dist[i][j];
bi = i;
bj = j;
}
}
}
vis[bi][bj] = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dist[i][j] = min(dist[i][j], dist[bi][bj] + max((abs(i - bi) + k - 2) / k,
(abs(j - bj) + k - 2) / k));
}
}
}
vis = vector<vector<bool>>(n, vector<bool>(m, false));
q.push({i1, j1});
vis[i1][j1] = true;
while (q.size()) {
int i = q.front()[0], j = q.front()[1];
q.pop();
for (int d = 0; d < 4; d++) {
int ni = i + dx[d], nj = j + dy[d];
if (!ok(ni, nj)) continue;
if (a[ni][nj] == '.') {
dist[i1][j1] = min(dist[i1][j1], dist[ni][nj]);
vis[i][j] = true;
q.push({ni, nj});
}
}
}
cout << dist[i1][j1] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
448 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
976 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 KB |
Output is correct |
25 |
Correct |
2 ms |
1112 KB |
Output is correct |
26 |
Correct |
4 ms |
800 KB |
Output is correct |
27 |
Correct |
4 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
856 KB |
Output is correct |
29 |
Correct |
6 ms |
1368 KB |
Output is correct |
30 |
Correct |
3 ms |
856 KB |
Output is correct |
31 |
Correct |
6 ms |
2136 KB |
Output is correct |
32 |
Correct |
9 ms |
1624 KB |
Output is correct |
33 |
Correct |
5 ms |
1372 KB |
Output is correct |
34 |
Correct |
4 ms |
2024 KB |
Output is correct |
35 |
Correct |
4 ms |
2024 KB |
Output is correct |
36 |
Correct |
9 ms |
1440 KB |
Output is correct |
37 |
Correct |
9 ms |
1372 KB |
Output is correct |
38 |
Correct |
6 ms |
1372 KB |
Output is correct |
39 |
Correct |
60 ms |
9472 KB |
Output is correct |
40 |
Correct |
11 ms |
1488 KB |
Output is correct |
41 |
Correct |
5 ms |
2264 KB |
Output is correct |
42 |
Correct |
10 ms |
1628 KB |
Output is correct |
43 |
Correct |
10 ms |
2396 KB |
Output is correct |
44 |
Correct |
40 ms |
5468 KB |
Output is correct |
45 |
Correct |
35 ms |
5944 KB |
Output is correct |
46 |
Correct |
50 ms |
15184 KB |
Output is correct |
47 |
Correct |
89 ms |
9496 KB |
Output is correct |
48 |
Correct |
55 ms |
9308 KB |
Output is correct |
49 |
Correct |
37 ms |
15104 KB |
Output is correct |
50 |
Correct |
41 ms |
15100 KB |
Output is correct |
51 |
Correct |
91 ms |
9820 KB |
Output is correct |
52 |
Correct |
88 ms |
9560 KB |
Output is correct |
53 |
Correct |
56 ms |
9456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
452 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
452 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
452 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
448 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
976 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 KB |
Output is correct |
25 |
Correct |
2 ms |
1112 KB |
Output is correct |
26 |
Correct |
4 ms |
800 KB |
Output is correct |
27 |
Correct |
4 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
856 KB |
Output is correct |
29 |
Correct |
6 ms |
1368 KB |
Output is correct |
30 |
Correct |
3 ms |
856 KB |
Output is correct |
31 |
Correct |
6 ms |
2136 KB |
Output is correct |
32 |
Correct |
9 ms |
1624 KB |
Output is correct |
33 |
Correct |
5 ms |
1372 KB |
Output is correct |
34 |
Correct |
4 ms |
2024 KB |
Output is correct |
35 |
Correct |
4 ms |
2024 KB |
Output is correct |
36 |
Correct |
9 ms |
1440 KB |
Output is correct |
37 |
Correct |
9 ms |
1372 KB |
Output is correct |
38 |
Correct |
6 ms |
1372 KB |
Output is correct |
39 |
Correct |
60 ms |
9472 KB |
Output is correct |
40 |
Correct |
11 ms |
1488 KB |
Output is correct |
41 |
Correct |
5 ms |
2264 KB |
Output is correct |
42 |
Correct |
10 ms |
1628 KB |
Output is correct |
43 |
Correct |
10 ms |
2396 KB |
Output is correct |
44 |
Correct |
40 ms |
5468 KB |
Output is correct |
45 |
Correct |
35 ms |
5944 KB |
Output is correct |
46 |
Correct |
50 ms |
15184 KB |
Output is correct |
47 |
Correct |
89 ms |
9496 KB |
Output is correct |
48 |
Correct |
55 ms |
9308 KB |
Output is correct |
49 |
Correct |
37 ms |
15104 KB |
Output is correct |
50 |
Correct |
41 ms |
15100 KB |
Output is correct |
51 |
Correct |
91 ms |
9820 KB |
Output is correct |
52 |
Correct |
88 ms |
9560 KB |
Output is correct |
53 |
Correct |
56 ms |
9456 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
452 KB |
Output is correct |
56 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
448 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
976 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 KB |
Output is correct |
25 |
Correct |
2 ms |
1112 KB |
Output is correct |
26 |
Correct |
4 ms |
800 KB |
Output is correct |
27 |
Correct |
4 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
856 KB |
Output is correct |
29 |
Correct |
6 ms |
1368 KB |
Output is correct |
30 |
Correct |
3 ms |
856 KB |
Output is correct |
31 |
Correct |
6 ms |
2136 KB |
Output is correct |
32 |
Correct |
9 ms |
1624 KB |
Output is correct |
33 |
Correct |
5 ms |
1372 KB |
Output is correct |
34 |
Correct |
4 ms |
2024 KB |
Output is correct |
35 |
Correct |
4 ms |
2024 KB |
Output is correct |
36 |
Correct |
9 ms |
1440 KB |
Output is correct |
37 |
Correct |
9 ms |
1372 KB |
Output is correct |
38 |
Correct |
6 ms |
1372 KB |
Output is correct |
39 |
Correct |
60 ms |
9472 KB |
Output is correct |
40 |
Correct |
11 ms |
1488 KB |
Output is correct |
41 |
Correct |
5 ms |
2264 KB |
Output is correct |
42 |
Correct |
10 ms |
1628 KB |
Output is correct |
43 |
Correct |
10 ms |
2396 KB |
Output is correct |
44 |
Correct |
40 ms |
5468 KB |
Output is correct |
45 |
Correct |
35 ms |
5944 KB |
Output is correct |
46 |
Correct |
50 ms |
15184 KB |
Output is correct |
47 |
Correct |
89 ms |
9496 KB |
Output is correct |
48 |
Correct |
55 ms |
9308 KB |
Output is correct |
49 |
Correct |
37 ms |
15104 KB |
Output is correct |
50 |
Correct |
41 ms |
15100 KB |
Output is correct |
51 |
Correct |
91 ms |
9820 KB |
Output is correct |
52 |
Correct |
88 ms |
9560 KB |
Output is correct |
53 |
Correct |
56 ms |
9456 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
452 KB |
Output is correct |
56 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
448 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
976 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 KB |
Output is correct |
25 |
Correct |
2 ms |
1112 KB |
Output is correct |
26 |
Correct |
4 ms |
800 KB |
Output is correct |
27 |
Correct |
4 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
856 KB |
Output is correct |
29 |
Correct |
6 ms |
1368 KB |
Output is correct |
30 |
Correct |
3 ms |
856 KB |
Output is correct |
31 |
Correct |
6 ms |
2136 KB |
Output is correct |
32 |
Correct |
9 ms |
1624 KB |
Output is correct |
33 |
Correct |
5 ms |
1372 KB |
Output is correct |
34 |
Correct |
4 ms |
2024 KB |
Output is correct |
35 |
Correct |
4 ms |
2024 KB |
Output is correct |
36 |
Correct |
9 ms |
1440 KB |
Output is correct |
37 |
Correct |
9 ms |
1372 KB |
Output is correct |
38 |
Correct |
6 ms |
1372 KB |
Output is correct |
39 |
Correct |
60 ms |
9472 KB |
Output is correct |
40 |
Correct |
11 ms |
1488 KB |
Output is correct |
41 |
Correct |
5 ms |
2264 KB |
Output is correct |
42 |
Correct |
10 ms |
1628 KB |
Output is correct |
43 |
Correct |
10 ms |
2396 KB |
Output is correct |
44 |
Correct |
40 ms |
5468 KB |
Output is correct |
45 |
Correct |
35 ms |
5944 KB |
Output is correct |
46 |
Correct |
50 ms |
15184 KB |
Output is correct |
47 |
Correct |
89 ms |
9496 KB |
Output is correct |
48 |
Correct |
55 ms |
9308 KB |
Output is correct |
49 |
Correct |
37 ms |
15104 KB |
Output is correct |
50 |
Correct |
41 ms |
15100 KB |
Output is correct |
51 |
Correct |
91 ms |
9820 KB |
Output is correct |
52 |
Correct |
88 ms |
9560 KB |
Output is correct |
53 |
Correct |
56 ms |
9456 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
452 KB |
Output is correct |
56 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |