#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 6e6 + 3;
int n, m, s, rs, cs, re, ce;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector <int> f[N], a[N], p[2][N];
queue <ii> q[2];
int get(int t, int id, int u) {
return p[t][id][u] == u ? u : p[t][id][u] = get(t, id, p[t][id][u]);
}
void upd(int t, int id, int i, int j, int val) {
int u = get(t, id, i);
while (u <= j) {
if(t) f[id][u] = val;
else f[u][id] = val;
u = get(t, id, u + 1);
p[t][id][i] = u;
}
}
main () {
cin.tie(0)->sync_with_stdio(0);
if(fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("wa.out", "w", stdout);
}
cin >> m >> n >> s >> rs >> cs >> re >> ce;
for(int i = 1; i <= m; i++) a[i].resize(n + 1), f[i].resize(n + 1);
for(int i = 1; i <= m; i++) {
for(int j = 0; j <= n + 1; j++) p[1][i].push_back(j);
}
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= m + 1; j++) p[0][i].push_back(j);
}
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= n; j++) {
char ch; cin >> ch;
a[i][j] = (ch == '.' ? 1 : 0);
}
}
q[1].push({rs, cs}); f[rs][cs] = 1;
while (!q[1].empty() || !q[0].empty()) {
while (!q[1].empty()) {
int x, y; tie(x, y) = q[1].front(); q[1].pop();
q[0].push({x, y});
if(x == re && y == ce) return cout << f[x][y] - 1 << "\n", 0;
for(int i = 0; i < 4; i++) {
int u = x + dx[i], v = y + dy[i];
if(u && u <= m && v && v <= n && !f[u][v] && a[u][v]) {
f[u][v] = f[x][y];
q[1].push({u, v});
}
}
}
while (!q[0].empty()) {
int x, y; tie(x, y) = q[0].front(); q[0].pop();
if(s != 1) {
if(x == rs && y == cs) {
int s1 = max(1, x - s), s2 = max(1, y - s);
int s3 = min(m, x + s), s4 = min(n, y + s);
for(int i = s1; i <= s3; i++) {
for(int j = s2; j <= s4; j++) if(!f[i][j]) {
f[i][j] = f[x][y] + 1;
q[1].push({i, j});
}
}
continue;
}
for(int i = 0; i < 4; i++) {
int u = x + dx[i], v = y + dy[i], val = f[x][y] + 1;
if(i == 0) upd(0, min(n, y + s), max(1, x - s), min(m, x + s), val);
if(i == 1) upd(1, min(m, x + s), max(1, y - s), min(n, y + s), val);
if(i == 2) upd(0, max(1, y - s), max(1, x - s), min(m, x + s), val);
if(i == 3) upd(1, max(1, x - s), max(1, y - s), min(n, y + s), val);
break ;
}
}
else {
for(int i = 0; i < 4; i++) {
int u = x + dx[i], v = y + dy[i];
if(u && u <= m && v && v <= n && !f[u][v]) {
f[u][v] = f[x][y] + 1;
q[1].push({u, v});
}
}
}
}
}
}
Compilation message
Main.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
28 | main () {
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:77:25: warning: unused variable 'u' [-Wunused-variable]
77 | int u = x + dx[i], v = y + dy[i], val = f[x][y] + 1;
| ^
Main.cpp:77:40: warning: unused variable 'v' [-Wunused-variable]
77 | int u = x + dx[i], v = y + dy[i], val = f[x][y] + 1;
| ^
Main.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen("task.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen("wa.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
563900 KB |
Output is correct |
2 |
Correct |
271 ms |
563884 KB |
Output is correct |
3 |
Correct |
282 ms |
563916 KB |
Output is correct |
4 |
Correct |
270 ms |
563904 KB |
Output is correct |
5 |
Correct |
272 ms |
563844 KB |
Output is correct |
6 |
Correct |
258 ms |
563896 KB |
Output is correct |
7 |
Correct |
289 ms |
563940 KB |
Output is correct |
8 |
Correct |
265 ms |
563844 KB |
Output is correct |
9 |
Correct |
270 ms |
563792 KB |
Output is correct |
10 |
Correct |
276 ms |
563820 KB |
Output is correct |
11 |
Correct |
278 ms |
563860 KB |
Output is correct |
12 |
Correct |
274 ms |
563788 KB |
Output is correct |
13 |
Correct |
281 ms |
563904 KB |
Output is correct |
14 |
Correct |
275 ms |
563816 KB |
Output is correct |
15 |
Correct |
260 ms |
563880 KB |
Output is correct |
16 |
Correct |
277 ms |
563936 KB |
Output is correct |
17 |
Correct |
274 ms |
563888 KB |
Output is correct |
18 |
Correct |
261 ms |
563936 KB |
Output is correct |
19 |
Correct |
273 ms |
564812 KB |
Output is correct |
20 |
Correct |
264 ms |
565812 KB |
Output is correct |
21 |
Correct |
259 ms |
565388 KB |
Output is correct |
22 |
Correct |
276 ms |
564896 KB |
Output is correct |
23 |
Correct |
272 ms |
564824 KB |
Output is correct |
24 |
Correct |
295 ms |
566448 KB |
Output is correct |
25 |
Correct |
274 ms |
566484 KB |
Output is correct |
26 |
Correct |
277 ms |
564888 KB |
Output is correct |
27 |
Correct |
259 ms |
564896 KB |
Output is correct |
28 |
Correct |
271 ms |
564824 KB |
Output is correct |
29 |
Correct |
265 ms |
566604 KB |
Output is correct |
30 |
Correct |
279 ms |
565528 KB |
Output is correct |
31 |
Correct |
300 ms |
567752 KB |
Output is correct |
32 |
Correct |
274 ms |
566684 KB |
Output is correct |
33 |
Correct |
268 ms |
566712 KB |
Output is correct |
34 |
Correct |
282 ms |
570312 KB |
Output is correct |
35 |
Correct |
282 ms |
570320 KB |
Output is correct |
36 |
Correct |
267 ms |
566880 KB |
Output is correct |
37 |
Correct |
289 ms |
566724 KB |
Output is correct |
38 |
Correct |
269 ms |
566676 KB |
Output is correct |
39 |
Correct |
399 ms |
595360 KB |
Output is correct |
40 |
Correct |
284 ms |
567320 KB |
Output is correct |
41 |
Correct |
279 ms |
571788 KB |
Output is correct |
42 |
Correct |
282 ms |
568260 KB |
Output is correct |
43 |
Correct |
285 ms |
570500 KB |
Output is correct |
44 |
Correct |
325 ms |
578528 KB |
Output is correct |
45 |
Correct |
311 ms |
580872 KB |
Output is correct |
46 |
Correct |
325 ms |
598196 KB |
Output is correct |
47 |
Correct |
383 ms |
595364 KB |
Output is correct |
48 |
Correct |
368 ms |
595360 KB |
Output is correct |
49 |
Correct |
486 ms |
628496 KB |
Output is correct |
50 |
Correct |
496 ms |
628464 KB |
Output is correct |
51 |
Correct |
395 ms |
595916 KB |
Output is correct |
52 |
Correct |
351 ms |
595396 KB |
Output is correct |
53 |
Correct |
397 ms |
595276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
249 ms |
563916 KB |
Output is correct |
2 |
Correct |
256 ms |
563880 KB |
Output is correct |
3 |
Correct |
250 ms |
563848 KB |
Output is correct |
4 |
Correct |
262 ms |
563904 KB |
Output is correct |
5 |
Correct |
265 ms |
563928 KB |
Output is correct |
6 |
Correct |
259 ms |
563848 KB |
Output is correct |
7 |
Correct |
262 ms |
563856 KB |
Output is correct |
8 |
Incorrect |
260 ms |
563912 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
267 ms |
563860 KB |
Output is correct |
2 |
Correct |
256 ms |
563800 KB |
Output is correct |
3 |
Correct |
268 ms |
563844 KB |
Output is correct |
4 |
Correct |
281 ms |
563896 KB |
Output is correct |
5 |
Correct |
255 ms |
563944 KB |
Output is correct |
6 |
Correct |
262 ms |
563796 KB |
Output is correct |
7 |
Correct |
260 ms |
563924 KB |
Output is correct |
8 |
Correct |
252 ms |
563848 KB |
Output is correct |
9 |
Correct |
256 ms |
563916 KB |
Output is correct |
10 |
Correct |
254 ms |
563920 KB |
Output is correct |
11 |
Correct |
251 ms |
563948 KB |
Output is correct |
12 |
Correct |
255 ms |
563956 KB |
Output is correct |
13 |
Correct |
251 ms |
563888 KB |
Output is correct |
14 |
Incorrect |
262 ms |
563836 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
249 ms |
563916 KB |
Output is correct |
2 |
Correct |
256 ms |
563880 KB |
Output is correct |
3 |
Correct |
250 ms |
563848 KB |
Output is correct |
4 |
Correct |
262 ms |
563904 KB |
Output is correct |
5 |
Correct |
265 ms |
563928 KB |
Output is correct |
6 |
Correct |
259 ms |
563848 KB |
Output is correct |
7 |
Correct |
262 ms |
563856 KB |
Output is correct |
8 |
Incorrect |
260 ms |
563912 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
249 ms |
563916 KB |
Output is correct |
2 |
Correct |
256 ms |
563880 KB |
Output is correct |
3 |
Correct |
250 ms |
563848 KB |
Output is correct |
4 |
Correct |
262 ms |
563904 KB |
Output is correct |
5 |
Correct |
265 ms |
563928 KB |
Output is correct |
6 |
Correct |
259 ms |
563848 KB |
Output is correct |
7 |
Correct |
262 ms |
563856 KB |
Output is correct |
8 |
Incorrect |
260 ms |
563912 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
563900 KB |
Output is correct |
2 |
Correct |
271 ms |
563884 KB |
Output is correct |
3 |
Correct |
282 ms |
563916 KB |
Output is correct |
4 |
Correct |
270 ms |
563904 KB |
Output is correct |
5 |
Correct |
272 ms |
563844 KB |
Output is correct |
6 |
Correct |
258 ms |
563896 KB |
Output is correct |
7 |
Correct |
289 ms |
563940 KB |
Output is correct |
8 |
Correct |
265 ms |
563844 KB |
Output is correct |
9 |
Correct |
270 ms |
563792 KB |
Output is correct |
10 |
Correct |
276 ms |
563820 KB |
Output is correct |
11 |
Correct |
278 ms |
563860 KB |
Output is correct |
12 |
Correct |
274 ms |
563788 KB |
Output is correct |
13 |
Correct |
281 ms |
563904 KB |
Output is correct |
14 |
Correct |
275 ms |
563816 KB |
Output is correct |
15 |
Correct |
260 ms |
563880 KB |
Output is correct |
16 |
Correct |
277 ms |
563936 KB |
Output is correct |
17 |
Correct |
274 ms |
563888 KB |
Output is correct |
18 |
Correct |
261 ms |
563936 KB |
Output is correct |
19 |
Correct |
273 ms |
564812 KB |
Output is correct |
20 |
Correct |
264 ms |
565812 KB |
Output is correct |
21 |
Correct |
259 ms |
565388 KB |
Output is correct |
22 |
Correct |
276 ms |
564896 KB |
Output is correct |
23 |
Correct |
272 ms |
564824 KB |
Output is correct |
24 |
Correct |
295 ms |
566448 KB |
Output is correct |
25 |
Correct |
274 ms |
566484 KB |
Output is correct |
26 |
Correct |
277 ms |
564888 KB |
Output is correct |
27 |
Correct |
259 ms |
564896 KB |
Output is correct |
28 |
Correct |
271 ms |
564824 KB |
Output is correct |
29 |
Correct |
265 ms |
566604 KB |
Output is correct |
30 |
Correct |
279 ms |
565528 KB |
Output is correct |
31 |
Correct |
300 ms |
567752 KB |
Output is correct |
32 |
Correct |
274 ms |
566684 KB |
Output is correct |
33 |
Correct |
268 ms |
566712 KB |
Output is correct |
34 |
Correct |
282 ms |
570312 KB |
Output is correct |
35 |
Correct |
282 ms |
570320 KB |
Output is correct |
36 |
Correct |
267 ms |
566880 KB |
Output is correct |
37 |
Correct |
289 ms |
566724 KB |
Output is correct |
38 |
Correct |
269 ms |
566676 KB |
Output is correct |
39 |
Correct |
399 ms |
595360 KB |
Output is correct |
40 |
Correct |
284 ms |
567320 KB |
Output is correct |
41 |
Correct |
279 ms |
571788 KB |
Output is correct |
42 |
Correct |
282 ms |
568260 KB |
Output is correct |
43 |
Correct |
285 ms |
570500 KB |
Output is correct |
44 |
Correct |
325 ms |
578528 KB |
Output is correct |
45 |
Correct |
311 ms |
580872 KB |
Output is correct |
46 |
Correct |
325 ms |
598196 KB |
Output is correct |
47 |
Correct |
383 ms |
595364 KB |
Output is correct |
48 |
Correct |
368 ms |
595360 KB |
Output is correct |
49 |
Correct |
486 ms |
628496 KB |
Output is correct |
50 |
Correct |
496 ms |
628464 KB |
Output is correct |
51 |
Correct |
395 ms |
595916 KB |
Output is correct |
52 |
Correct |
351 ms |
595396 KB |
Output is correct |
53 |
Correct |
397 ms |
595276 KB |
Output is correct |
54 |
Correct |
249 ms |
563916 KB |
Output is correct |
55 |
Correct |
256 ms |
563880 KB |
Output is correct |
56 |
Correct |
250 ms |
563848 KB |
Output is correct |
57 |
Correct |
262 ms |
563904 KB |
Output is correct |
58 |
Correct |
265 ms |
563928 KB |
Output is correct |
59 |
Correct |
259 ms |
563848 KB |
Output is correct |
60 |
Correct |
262 ms |
563856 KB |
Output is correct |
61 |
Incorrect |
260 ms |
563912 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
563900 KB |
Output is correct |
2 |
Correct |
271 ms |
563884 KB |
Output is correct |
3 |
Correct |
282 ms |
563916 KB |
Output is correct |
4 |
Correct |
270 ms |
563904 KB |
Output is correct |
5 |
Correct |
272 ms |
563844 KB |
Output is correct |
6 |
Correct |
258 ms |
563896 KB |
Output is correct |
7 |
Correct |
289 ms |
563940 KB |
Output is correct |
8 |
Correct |
265 ms |
563844 KB |
Output is correct |
9 |
Correct |
270 ms |
563792 KB |
Output is correct |
10 |
Correct |
276 ms |
563820 KB |
Output is correct |
11 |
Correct |
278 ms |
563860 KB |
Output is correct |
12 |
Correct |
274 ms |
563788 KB |
Output is correct |
13 |
Correct |
281 ms |
563904 KB |
Output is correct |
14 |
Correct |
275 ms |
563816 KB |
Output is correct |
15 |
Correct |
260 ms |
563880 KB |
Output is correct |
16 |
Correct |
277 ms |
563936 KB |
Output is correct |
17 |
Correct |
274 ms |
563888 KB |
Output is correct |
18 |
Correct |
261 ms |
563936 KB |
Output is correct |
19 |
Correct |
273 ms |
564812 KB |
Output is correct |
20 |
Correct |
264 ms |
565812 KB |
Output is correct |
21 |
Correct |
259 ms |
565388 KB |
Output is correct |
22 |
Correct |
276 ms |
564896 KB |
Output is correct |
23 |
Correct |
272 ms |
564824 KB |
Output is correct |
24 |
Correct |
295 ms |
566448 KB |
Output is correct |
25 |
Correct |
274 ms |
566484 KB |
Output is correct |
26 |
Correct |
277 ms |
564888 KB |
Output is correct |
27 |
Correct |
259 ms |
564896 KB |
Output is correct |
28 |
Correct |
271 ms |
564824 KB |
Output is correct |
29 |
Correct |
265 ms |
566604 KB |
Output is correct |
30 |
Correct |
279 ms |
565528 KB |
Output is correct |
31 |
Correct |
300 ms |
567752 KB |
Output is correct |
32 |
Correct |
274 ms |
566684 KB |
Output is correct |
33 |
Correct |
268 ms |
566712 KB |
Output is correct |
34 |
Correct |
282 ms |
570312 KB |
Output is correct |
35 |
Correct |
282 ms |
570320 KB |
Output is correct |
36 |
Correct |
267 ms |
566880 KB |
Output is correct |
37 |
Correct |
289 ms |
566724 KB |
Output is correct |
38 |
Correct |
269 ms |
566676 KB |
Output is correct |
39 |
Correct |
399 ms |
595360 KB |
Output is correct |
40 |
Correct |
284 ms |
567320 KB |
Output is correct |
41 |
Correct |
279 ms |
571788 KB |
Output is correct |
42 |
Correct |
282 ms |
568260 KB |
Output is correct |
43 |
Correct |
285 ms |
570500 KB |
Output is correct |
44 |
Correct |
325 ms |
578528 KB |
Output is correct |
45 |
Correct |
311 ms |
580872 KB |
Output is correct |
46 |
Correct |
325 ms |
598196 KB |
Output is correct |
47 |
Correct |
383 ms |
595364 KB |
Output is correct |
48 |
Correct |
368 ms |
595360 KB |
Output is correct |
49 |
Correct |
486 ms |
628496 KB |
Output is correct |
50 |
Correct |
496 ms |
628464 KB |
Output is correct |
51 |
Correct |
395 ms |
595916 KB |
Output is correct |
52 |
Correct |
351 ms |
595396 KB |
Output is correct |
53 |
Correct |
397 ms |
595276 KB |
Output is correct |
54 |
Correct |
249 ms |
563916 KB |
Output is correct |
55 |
Correct |
256 ms |
563880 KB |
Output is correct |
56 |
Correct |
250 ms |
563848 KB |
Output is correct |
57 |
Correct |
262 ms |
563904 KB |
Output is correct |
58 |
Correct |
265 ms |
563928 KB |
Output is correct |
59 |
Correct |
259 ms |
563848 KB |
Output is correct |
60 |
Correct |
262 ms |
563856 KB |
Output is correct |
61 |
Incorrect |
260 ms |
563912 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
563900 KB |
Output is correct |
2 |
Correct |
271 ms |
563884 KB |
Output is correct |
3 |
Correct |
282 ms |
563916 KB |
Output is correct |
4 |
Correct |
270 ms |
563904 KB |
Output is correct |
5 |
Correct |
272 ms |
563844 KB |
Output is correct |
6 |
Correct |
258 ms |
563896 KB |
Output is correct |
7 |
Correct |
289 ms |
563940 KB |
Output is correct |
8 |
Correct |
265 ms |
563844 KB |
Output is correct |
9 |
Correct |
270 ms |
563792 KB |
Output is correct |
10 |
Correct |
276 ms |
563820 KB |
Output is correct |
11 |
Correct |
278 ms |
563860 KB |
Output is correct |
12 |
Correct |
274 ms |
563788 KB |
Output is correct |
13 |
Correct |
281 ms |
563904 KB |
Output is correct |
14 |
Correct |
275 ms |
563816 KB |
Output is correct |
15 |
Correct |
260 ms |
563880 KB |
Output is correct |
16 |
Correct |
277 ms |
563936 KB |
Output is correct |
17 |
Correct |
274 ms |
563888 KB |
Output is correct |
18 |
Correct |
261 ms |
563936 KB |
Output is correct |
19 |
Correct |
273 ms |
564812 KB |
Output is correct |
20 |
Correct |
264 ms |
565812 KB |
Output is correct |
21 |
Correct |
259 ms |
565388 KB |
Output is correct |
22 |
Correct |
276 ms |
564896 KB |
Output is correct |
23 |
Correct |
272 ms |
564824 KB |
Output is correct |
24 |
Correct |
295 ms |
566448 KB |
Output is correct |
25 |
Correct |
274 ms |
566484 KB |
Output is correct |
26 |
Correct |
277 ms |
564888 KB |
Output is correct |
27 |
Correct |
259 ms |
564896 KB |
Output is correct |
28 |
Correct |
271 ms |
564824 KB |
Output is correct |
29 |
Correct |
265 ms |
566604 KB |
Output is correct |
30 |
Correct |
279 ms |
565528 KB |
Output is correct |
31 |
Correct |
300 ms |
567752 KB |
Output is correct |
32 |
Correct |
274 ms |
566684 KB |
Output is correct |
33 |
Correct |
268 ms |
566712 KB |
Output is correct |
34 |
Correct |
282 ms |
570312 KB |
Output is correct |
35 |
Correct |
282 ms |
570320 KB |
Output is correct |
36 |
Correct |
267 ms |
566880 KB |
Output is correct |
37 |
Correct |
289 ms |
566724 KB |
Output is correct |
38 |
Correct |
269 ms |
566676 KB |
Output is correct |
39 |
Correct |
399 ms |
595360 KB |
Output is correct |
40 |
Correct |
284 ms |
567320 KB |
Output is correct |
41 |
Correct |
279 ms |
571788 KB |
Output is correct |
42 |
Correct |
282 ms |
568260 KB |
Output is correct |
43 |
Correct |
285 ms |
570500 KB |
Output is correct |
44 |
Correct |
325 ms |
578528 KB |
Output is correct |
45 |
Correct |
311 ms |
580872 KB |
Output is correct |
46 |
Correct |
325 ms |
598196 KB |
Output is correct |
47 |
Correct |
383 ms |
595364 KB |
Output is correct |
48 |
Correct |
368 ms |
595360 KB |
Output is correct |
49 |
Correct |
486 ms |
628496 KB |
Output is correct |
50 |
Correct |
496 ms |
628464 KB |
Output is correct |
51 |
Correct |
395 ms |
595916 KB |
Output is correct |
52 |
Correct |
351 ms |
595396 KB |
Output is correct |
53 |
Correct |
397 ms |
595276 KB |
Output is correct |
54 |
Correct |
249 ms |
563916 KB |
Output is correct |
55 |
Correct |
256 ms |
563880 KB |
Output is correct |
56 |
Correct |
250 ms |
563848 KB |
Output is correct |
57 |
Correct |
262 ms |
563904 KB |
Output is correct |
58 |
Correct |
265 ms |
563928 KB |
Output is correct |
59 |
Correct |
259 ms |
563848 KB |
Output is correct |
60 |
Correct |
262 ms |
563856 KB |
Output is correct |
61 |
Incorrect |
260 ms |
563912 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |