#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
616 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
356 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
424 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 ms |
608 KB |
Output is correct |
17 |
Correct |
0 ms |
608 KB |
Output is correct |
18 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
608 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
396 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
356 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
0 ms |
352 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
352 KB |
Output is correct |
25 |
Correct |
0 ms |
352 KB |
Output is correct |
26 |
Correct |
0 ms |
356 KB |
Output is correct |
27 |
Correct |
0 ms |
356 KB |
Output is correct |
28 |
Correct |
0 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
352 KB |
Output is correct |
30 |
Correct |
0 ms |
608 KB |
Output is correct |
31 |
Correct |
0 ms |
604 KB |
Output is correct |
32 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
356 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
612 KB |
Output is correct |
8 |
Correct |
0 ms |
612 KB |
Output is correct |
9 |
Correct |
1 ms |
608 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
356 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
356 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
356 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
396 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
5 ms |
1844 KB |
Output is correct |
28 |
Correct |
2 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
1632 KB |
Output is correct |
30 |
Correct |
1 ms |
1628 KB |
Output is correct |
31 |
Correct |
20 ms |
1116 KB |
Output is correct |
32 |
Correct |
3 ms |
1880 KB |
Output is correct |
33 |
Correct |
2 ms |
1884 KB |
Output is correct |
34 |
Correct |
5 ms |
1760 KB |
Output is correct |
35 |
Correct |
13 ms |
2652 KB |
Output is correct |
36 |
Correct |
4 ms |
2620 KB |
Output is correct |
37 |
Correct |
3 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2396 KB |
Output is correct |
39 |
Correct |
9 ms |
4188 KB |
Output is correct |
40 |
Incorrect |
235 ms |
3280 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
608 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
396 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
356 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
0 ms |
352 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
352 KB |
Output is correct |
25 |
Correct |
0 ms |
352 KB |
Output is correct |
26 |
Correct |
0 ms |
356 KB |
Output is correct |
27 |
Correct |
0 ms |
356 KB |
Output is correct |
28 |
Correct |
0 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
352 KB |
Output is correct |
30 |
Correct |
0 ms |
608 KB |
Output is correct |
31 |
Correct |
0 ms |
604 KB |
Output is correct |
32 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
608 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
396 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
356 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
0 ms |
352 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
352 KB |
Output is correct |
25 |
Correct |
0 ms |
352 KB |
Output is correct |
26 |
Correct |
0 ms |
356 KB |
Output is correct |
27 |
Correct |
0 ms |
356 KB |
Output is correct |
28 |
Correct |
0 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
352 KB |
Output is correct |
30 |
Correct |
0 ms |
608 KB |
Output is correct |
31 |
Correct |
0 ms |
604 KB |
Output is correct |
32 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
616 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
356 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
424 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 ms |
608 KB |
Output is correct |
17 |
Correct |
0 ms |
608 KB |
Output is correct |
18 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
616 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
356 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
424 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 ms |
608 KB |
Output is correct |
17 |
Correct |
0 ms |
608 KB |
Output is correct |
18 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
616 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
356 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
424 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 ms |
608 KB |
Output is correct |
17 |
Correct |
0 ms |
608 KB |
Output is correct |
18 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |