#include <bits/stdc++.h>
using namespace std;
#ifdef MIKU
#define debug(x...) cout << "[" << #x << "] : ", dout(x)
void dout() { cout << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
// #define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;
const int MXN = 6000005, LAYER = 8;
const pii D[4] = {mp(1, 0), mp(-1, 0), mp(0, 1), mp(0, -1)};
int R, C, N;
pii sr, to;
vector<string> s;
int K;
vector<int> edge[MXN * LAYER];
int dis[MXN * LAYER];
inline int cvt(pii p) {
return p.fs * C + p.sc;
}
inline pii cvt(int x) {
return mp(x / C, x % C);
}
inline pii operator+(pii a, pii b) {
return mp(a.fs + b.fs, a.sc + b.sc);
}
inline bool OUT(pii p) {
if (!(0 <= p.fs && p.fs < R)) return true;
if (!(0 <= p.sc && p.sc < C)) return true;
return false;
}
namespace EDGE {
inline int cvt3(int l, int x, int y) {
return l * R * C + x * C + y;
}
inline void PUSH_EDGE(int sr, int to, int val) {
// debug(sr, to, val);
edge[sr].push_back(val ? ~to : to);
}
void E0() {
FOR(i, 0, R * C) {
pii p = cvt(i);
if (s[p.fs][p.sc] == '#') continue;
FOR(d, 0, 4) {
pii nxt = p + D[d];
if (OUT(nxt)) continue;
if (s[nxt.fs][nxt.sc] == '#') continue;
PUSH_EDGE(cvt3(0, p.fs, p.sc), cvt3(0, nxt.fs, nxt.sc), 0);
}
}
}
void E0_1() {
FOR(i, 0, R * C) {
pii p = cvt(i);
FOR(d, 0, 4) {
pii nxt = p + D[d];
if (OUT(nxt)) continue;
PUSH_EDGE(cvt3(0, p.fs, p.sc), cvt3(1, nxt.fs, nxt.sc), 1);
}
}
}
void E23_4() {
FOR(j, 0, C) {
for (int l_bnd = 0, r_bnd; l_bnd < R; l_bnd += K) {
r_bnd = min(R, l_bnd + K);
FOR(i, l_bnd, r_bnd) {
PUSH_EDGE(cvt3(2, i, j), cvt3(4, i, j), 0);
PUSH_EDGE(cvt3(3, i, j), cvt3(4, i, j), 0);
}
FOR(i, l_bnd, r_bnd - 1) PUSH_EDGE(cvt3(2, i, j), cvt3(2, i + 1, j), 0);
FOR(i, l_bnd + 1, r_bnd) PUSH_EDGE(cvt3(3, i, j), cvt3(3, i - 1, j), 0);
}
}
}
void E1_23() {
auto ADD_EDGE = [&](int y, int l, int r, int sr) -> void {
if (r - l == K) {
PUSH_EDGE(sr, cvt3(2, l, y), 0);
PUSH_EDGE(sr, cvt3(3, r - 1, y), 0);
return;
}
if (r == R) {
PUSH_EDGE(sr, cvt3(2, l, y), 0);
int m = (l ? (l - 1) / K * K + K : 0);
if (m < R) PUSH_EDGE(sr, cvt3(2, m, y), 0);
return;
}
PUSH_EDGE(sr, cvt3(3, r - 1, y), 0);
};
FOR(j, 0, C) {
FOR(i, 0, R) {
int l_bnd = max(0, i - (N - 1)), r_bnd = min(R, i + N);
ADD_EDGE(j, l_bnd, r_bnd, cvt3(1, i, j));
}
}
}
void E56_7() {
FOR(i, 0, R) {
for (int l_bnd = 0, r_bnd; l_bnd < C; l_bnd += K) {
r_bnd = min(C, l_bnd + K);
FOR(j, l_bnd, r_bnd) {
PUSH_EDGE(cvt3(5, i, j), cvt3(7, i, j), 0);
PUSH_EDGE(cvt3(6, i, j), cvt3(7, i, j), 0);
}
FOR(j, l_bnd, r_bnd - 1) PUSH_EDGE(cvt3(5, i, j), cvt3(5, i, j + 1), 0);
FOR(j, l_bnd + 1, r_bnd) PUSH_EDGE(cvt3(6, i, j), cvt3(6, i, j - 1), 0);
}
}
}
void E4_56() {
auto ADD_EDGE = [&](int x, int l, int r, int sr) -> void {
if (r - l == K) {
PUSH_EDGE(sr, cvt3(5, x, l), 0);
PUSH_EDGE(sr, cvt3(6, x, r - 1), 0);
return;
}
if (r == C) {
PUSH_EDGE(sr, cvt3(5, x, l), 0);
int m = (l ? (l - 1) / K * K + K : 0);
if (m < C) PUSH_EDGE(sr, cvt3(5, x, m), 0);
return;
}
PUSH_EDGE(sr, cvt3(6, x, r - 1), 0);
};
FOR(i, 0, R) {
FOR(j, 0, C) {
int l_bnd = max(0, j - (N - 1)), r_bnd = min(C, j + N);
ADD_EDGE(i, l_bnd, r_bnd, cvt3(4, i, j));
}
}
}
void E7_0() {
FOR(i, 0, R * C) {
pii p = cvt(i);
FOR(d, 0, 4) {
pii nxt = p + D[d];
if (OUT(nxt) || s[nxt.fs][nxt.sc] == '#') continue;
PUSH_EDGE(cvt3(7, p.fs, p.sc), cvt3(0, nxt.fs, nxt.sc), 0);
}
}
}
void E00() {
FOR(i, 0, R * C) {
pii p = cvt(i);
FOR(d, 0, 4) {
pii nxt = p + D[d];
if (OUT(nxt)) continue;
// if s[nxt.fs][nxt.sc] == '#'
PUSH_EDGE(cvt(p), cvt(nxt), s[nxt.fs][nxt.sc] == '#');
}
}
}
void BUILD() {
// E0();
// E0_1();
// E23_4();
// E1_23();
// E56_7();
// E4_56();
// E7_0();
E00();
}
}
namespace BFS {
deque<pii> dq;
void GO(int sr) {
fill(dis, dis + R * C * LAYER, -1);
dq.push_back(mp(0, sr));
while (dq.size()) {
pii now = dq.front();
dq.pop_front();
// debug(now.fs, now.sc);
if (dis[now.sc] != -1) continue;
int len = now.fs, id = now.sc;
dis[id] = len;
for (auto &i : edge[id]) {
int tg = (i < 0 ? ~i : i);
if (dis[tg] != -1) continue;
if (i < 0) dq.push_back(mp(len + 1, tg));
else dq.push_front(mp(len, tg));
}
}
}
}
void miku() {
cin >> R >> C >> N >> sr.fs >> sr.sc >> to.fs >> to.sc;
sr.fs--, sr.sc--, to.fs--, to.sc--;
K = N * 2 - 1;
s.resize(R);
FOR(i, 0, R) cin >> s[i];
EDGE::BUILD();
BFS::GO(cvt(sr));
cout << dis[cvt(to)] << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(iostream::failbit);
miku();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
526 ms |
1128580 KB |
Output is correct |
2 |
Correct |
437 ms |
1128608 KB |
Output is correct |
3 |
Correct |
444 ms |
1128868 KB |
Output is correct |
4 |
Correct |
425 ms |
1128532 KB |
Output is correct |
5 |
Correct |
444 ms |
1128512 KB |
Output is correct |
6 |
Correct |
461 ms |
1128640 KB |
Output is correct |
7 |
Correct |
447 ms |
1128796 KB |
Output is correct |
8 |
Correct |
435 ms |
1128536 KB |
Output is correct |
9 |
Correct |
420 ms |
1128536 KB |
Output is correct |
10 |
Correct |
478 ms |
1128788 KB |
Output is correct |
11 |
Correct |
447 ms |
1128536 KB |
Output is correct |
12 |
Correct |
441 ms |
1128736 KB |
Output is correct |
13 |
Correct |
442 ms |
1128532 KB |
Output is correct |
14 |
Correct |
455 ms |
1128792 KB |
Output is correct |
15 |
Correct |
472 ms |
1128696 KB |
Output is correct |
16 |
Correct |
456 ms |
1128672 KB |
Output is correct |
17 |
Correct |
464 ms |
1128852 KB |
Output is correct |
18 |
Correct |
436 ms |
1128536 KB |
Output is correct |
19 |
Correct |
484 ms |
1132372 KB |
Output is correct |
20 |
Correct |
448 ms |
1131676 KB |
Output is correct |
21 |
Correct |
450 ms |
1132632 KB |
Output is correct |
22 |
Correct |
493 ms |
1132376 KB |
Output is correct |
23 |
Correct |
438 ms |
1132628 KB |
Output is correct |
24 |
Correct |
445 ms |
1132492 KB |
Output is correct |
25 |
Correct |
478 ms |
1132596 KB |
Output is correct |
26 |
Correct |
505 ms |
1132372 KB |
Output is correct |
27 |
Correct |
457 ms |
1132536 KB |
Output is correct |
28 |
Correct |
469 ms |
1132496 KB |
Output is correct |
29 |
Correct |
504 ms |
1138312 KB |
Output is correct |
30 |
Correct |
458 ms |
1132628 KB |
Output is correct |
31 |
Correct |
459 ms |
1139312 KB |
Output is correct |
32 |
Correct |
519 ms |
1138264 KB |
Output is correct |
33 |
Correct |
474 ms |
1138160 KB |
Output is correct |
34 |
Correct |
468 ms |
1138148 KB |
Output is correct |
35 |
Correct |
513 ms |
1138160 KB |
Output is correct |
36 |
Correct |
494 ms |
1138464 KB |
Output is correct |
37 |
Correct |
479 ms |
1138408 KB |
Output is correct |
38 |
Correct |
522 ms |
1138216 KB |
Output is correct |
39 |
Correct |
691 ms |
1225780 KB |
Output is correct |
40 |
Correct |
479 ms |
1139796 KB |
Output is correct |
41 |
Correct |
440 ms |
1140404 KB |
Output is correct |
42 |
Correct |
500 ms |
1142404 KB |
Output is correct |
43 |
Correct |
458 ms |
1144196 KB |
Output is correct |
44 |
Correct |
552 ms |
1176404 KB |
Output is correct |
45 |
Correct |
532 ms |
1180396 KB |
Output is correct |
46 |
Correct |
625 ms |
1236396 KB |
Output is correct |
47 |
Correct |
704 ms |
1225576 KB |
Output is correct |
48 |
Correct |
669 ms |
1225400 KB |
Output is correct |
49 |
Correct |
554 ms |
1225020 KB |
Output is correct |
50 |
Correct |
566 ms |
1225068 KB |
Output is correct |
51 |
Correct |
719 ms |
1225812 KB |
Output is correct |
52 |
Correct |
707 ms |
1225608 KB |
Output is correct |
53 |
Correct |
658 ms |
1225216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
419 ms |
1128936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
421 ms |
1128372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
419 ms |
1128936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
419 ms |
1128936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
526 ms |
1128580 KB |
Output is correct |
2 |
Correct |
437 ms |
1128608 KB |
Output is correct |
3 |
Correct |
444 ms |
1128868 KB |
Output is correct |
4 |
Correct |
425 ms |
1128532 KB |
Output is correct |
5 |
Correct |
444 ms |
1128512 KB |
Output is correct |
6 |
Correct |
461 ms |
1128640 KB |
Output is correct |
7 |
Correct |
447 ms |
1128796 KB |
Output is correct |
8 |
Correct |
435 ms |
1128536 KB |
Output is correct |
9 |
Correct |
420 ms |
1128536 KB |
Output is correct |
10 |
Correct |
478 ms |
1128788 KB |
Output is correct |
11 |
Correct |
447 ms |
1128536 KB |
Output is correct |
12 |
Correct |
441 ms |
1128736 KB |
Output is correct |
13 |
Correct |
442 ms |
1128532 KB |
Output is correct |
14 |
Correct |
455 ms |
1128792 KB |
Output is correct |
15 |
Correct |
472 ms |
1128696 KB |
Output is correct |
16 |
Correct |
456 ms |
1128672 KB |
Output is correct |
17 |
Correct |
464 ms |
1128852 KB |
Output is correct |
18 |
Correct |
436 ms |
1128536 KB |
Output is correct |
19 |
Correct |
484 ms |
1132372 KB |
Output is correct |
20 |
Correct |
448 ms |
1131676 KB |
Output is correct |
21 |
Correct |
450 ms |
1132632 KB |
Output is correct |
22 |
Correct |
493 ms |
1132376 KB |
Output is correct |
23 |
Correct |
438 ms |
1132628 KB |
Output is correct |
24 |
Correct |
445 ms |
1132492 KB |
Output is correct |
25 |
Correct |
478 ms |
1132596 KB |
Output is correct |
26 |
Correct |
505 ms |
1132372 KB |
Output is correct |
27 |
Correct |
457 ms |
1132536 KB |
Output is correct |
28 |
Correct |
469 ms |
1132496 KB |
Output is correct |
29 |
Correct |
504 ms |
1138312 KB |
Output is correct |
30 |
Correct |
458 ms |
1132628 KB |
Output is correct |
31 |
Correct |
459 ms |
1139312 KB |
Output is correct |
32 |
Correct |
519 ms |
1138264 KB |
Output is correct |
33 |
Correct |
474 ms |
1138160 KB |
Output is correct |
34 |
Correct |
468 ms |
1138148 KB |
Output is correct |
35 |
Correct |
513 ms |
1138160 KB |
Output is correct |
36 |
Correct |
494 ms |
1138464 KB |
Output is correct |
37 |
Correct |
479 ms |
1138408 KB |
Output is correct |
38 |
Correct |
522 ms |
1138216 KB |
Output is correct |
39 |
Correct |
691 ms |
1225780 KB |
Output is correct |
40 |
Correct |
479 ms |
1139796 KB |
Output is correct |
41 |
Correct |
440 ms |
1140404 KB |
Output is correct |
42 |
Correct |
500 ms |
1142404 KB |
Output is correct |
43 |
Correct |
458 ms |
1144196 KB |
Output is correct |
44 |
Correct |
552 ms |
1176404 KB |
Output is correct |
45 |
Correct |
532 ms |
1180396 KB |
Output is correct |
46 |
Correct |
625 ms |
1236396 KB |
Output is correct |
47 |
Correct |
704 ms |
1225576 KB |
Output is correct |
48 |
Correct |
669 ms |
1225400 KB |
Output is correct |
49 |
Correct |
554 ms |
1225020 KB |
Output is correct |
50 |
Correct |
566 ms |
1225068 KB |
Output is correct |
51 |
Correct |
719 ms |
1225812 KB |
Output is correct |
52 |
Correct |
707 ms |
1225608 KB |
Output is correct |
53 |
Correct |
658 ms |
1225216 KB |
Output is correct |
54 |
Incorrect |
419 ms |
1128936 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
526 ms |
1128580 KB |
Output is correct |
2 |
Correct |
437 ms |
1128608 KB |
Output is correct |
3 |
Correct |
444 ms |
1128868 KB |
Output is correct |
4 |
Correct |
425 ms |
1128532 KB |
Output is correct |
5 |
Correct |
444 ms |
1128512 KB |
Output is correct |
6 |
Correct |
461 ms |
1128640 KB |
Output is correct |
7 |
Correct |
447 ms |
1128796 KB |
Output is correct |
8 |
Correct |
435 ms |
1128536 KB |
Output is correct |
9 |
Correct |
420 ms |
1128536 KB |
Output is correct |
10 |
Correct |
478 ms |
1128788 KB |
Output is correct |
11 |
Correct |
447 ms |
1128536 KB |
Output is correct |
12 |
Correct |
441 ms |
1128736 KB |
Output is correct |
13 |
Correct |
442 ms |
1128532 KB |
Output is correct |
14 |
Correct |
455 ms |
1128792 KB |
Output is correct |
15 |
Correct |
472 ms |
1128696 KB |
Output is correct |
16 |
Correct |
456 ms |
1128672 KB |
Output is correct |
17 |
Correct |
464 ms |
1128852 KB |
Output is correct |
18 |
Correct |
436 ms |
1128536 KB |
Output is correct |
19 |
Correct |
484 ms |
1132372 KB |
Output is correct |
20 |
Correct |
448 ms |
1131676 KB |
Output is correct |
21 |
Correct |
450 ms |
1132632 KB |
Output is correct |
22 |
Correct |
493 ms |
1132376 KB |
Output is correct |
23 |
Correct |
438 ms |
1132628 KB |
Output is correct |
24 |
Correct |
445 ms |
1132492 KB |
Output is correct |
25 |
Correct |
478 ms |
1132596 KB |
Output is correct |
26 |
Correct |
505 ms |
1132372 KB |
Output is correct |
27 |
Correct |
457 ms |
1132536 KB |
Output is correct |
28 |
Correct |
469 ms |
1132496 KB |
Output is correct |
29 |
Correct |
504 ms |
1138312 KB |
Output is correct |
30 |
Correct |
458 ms |
1132628 KB |
Output is correct |
31 |
Correct |
459 ms |
1139312 KB |
Output is correct |
32 |
Correct |
519 ms |
1138264 KB |
Output is correct |
33 |
Correct |
474 ms |
1138160 KB |
Output is correct |
34 |
Correct |
468 ms |
1138148 KB |
Output is correct |
35 |
Correct |
513 ms |
1138160 KB |
Output is correct |
36 |
Correct |
494 ms |
1138464 KB |
Output is correct |
37 |
Correct |
479 ms |
1138408 KB |
Output is correct |
38 |
Correct |
522 ms |
1138216 KB |
Output is correct |
39 |
Correct |
691 ms |
1225780 KB |
Output is correct |
40 |
Correct |
479 ms |
1139796 KB |
Output is correct |
41 |
Correct |
440 ms |
1140404 KB |
Output is correct |
42 |
Correct |
500 ms |
1142404 KB |
Output is correct |
43 |
Correct |
458 ms |
1144196 KB |
Output is correct |
44 |
Correct |
552 ms |
1176404 KB |
Output is correct |
45 |
Correct |
532 ms |
1180396 KB |
Output is correct |
46 |
Correct |
625 ms |
1236396 KB |
Output is correct |
47 |
Correct |
704 ms |
1225576 KB |
Output is correct |
48 |
Correct |
669 ms |
1225400 KB |
Output is correct |
49 |
Correct |
554 ms |
1225020 KB |
Output is correct |
50 |
Correct |
566 ms |
1225068 KB |
Output is correct |
51 |
Correct |
719 ms |
1225812 KB |
Output is correct |
52 |
Correct |
707 ms |
1225608 KB |
Output is correct |
53 |
Correct |
658 ms |
1225216 KB |
Output is correct |
54 |
Incorrect |
419 ms |
1128936 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
526 ms |
1128580 KB |
Output is correct |
2 |
Correct |
437 ms |
1128608 KB |
Output is correct |
3 |
Correct |
444 ms |
1128868 KB |
Output is correct |
4 |
Correct |
425 ms |
1128532 KB |
Output is correct |
5 |
Correct |
444 ms |
1128512 KB |
Output is correct |
6 |
Correct |
461 ms |
1128640 KB |
Output is correct |
7 |
Correct |
447 ms |
1128796 KB |
Output is correct |
8 |
Correct |
435 ms |
1128536 KB |
Output is correct |
9 |
Correct |
420 ms |
1128536 KB |
Output is correct |
10 |
Correct |
478 ms |
1128788 KB |
Output is correct |
11 |
Correct |
447 ms |
1128536 KB |
Output is correct |
12 |
Correct |
441 ms |
1128736 KB |
Output is correct |
13 |
Correct |
442 ms |
1128532 KB |
Output is correct |
14 |
Correct |
455 ms |
1128792 KB |
Output is correct |
15 |
Correct |
472 ms |
1128696 KB |
Output is correct |
16 |
Correct |
456 ms |
1128672 KB |
Output is correct |
17 |
Correct |
464 ms |
1128852 KB |
Output is correct |
18 |
Correct |
436 ms |
1128536 KB |
Output is correct |
19 |
Correct |
484 ms |
1132372 KB |
Output is correct |
20 |
Correct |
448 ms |
1131676 KB |
Output is correct |
21 |
Correct |
450 ms |
1132632 KB |
Output is correct |
22 |
Correct |
493 ms |
1132376 KB |
Output is correct |
23 |
Correct |
438 ms |
1132628 KB |
Output is correct |
24 |
Correct |
445 ms |
1132492 KB |
Output is correct |
25 |
Correct |
478 ms |
1132596 KB |
Output is correct |
26 |
Correct |
505 ms |
1132372 KB |
Output is correct |
27 |
Correct |
457 ms |
1132536 KB |
Output is correct |
28 |
Correct |
469 ms |
1132496 KB |
Output is correct |
29 |
Correct |
504 ms |
1138312 KB |
Output is correct |
30 |
Correct |
458 ms |
1132628 KB |
Output is correct |
31 |
Correct |
459 ms |
1139312 KB |
Output is correct |
32 |
Correct |
519 ms |
1138264 KB |
Output is correct |
33 |
Correct |
474 ms |
1138160 KB |
Output is correct |
34 |
Correct |
468 ms |
1138148 KB |
Output is correct |
35 |
Correct |
513 ms |
1138160 KB |
Output is correct |
36 |
Correct |
494 ms |
1138464 KB |
Output is correct |
37 |
Correct |
479 ms |
1138408 KB |
Output is correct |
38 |
Correct |
522 ms |
1138216 KB |
Output is correct |
39 |
Correct |
691 ms |
1225780 KB |
Output is correct |
40 |
Correct |
479 ms |
1139796 KB |
Output is correct |
41 |
Correct |
440 ms |
1140404 KB |
Output is correct |
42 |
Correct |
500 ms |
1142404 KB |
Output is correct |
43 |
Correct |
458 ms |
1144196 KB |
Output is correct |
44 |
Correct |
552 ms |
1176404 KB |
Output is correct |
45 |
Correct |
532 ms |
1180396 KB |
Output is correct |
46 |
Correct |
625 ms |
1236396 KB |
Output is correct |
47 |
Correct |
704 ms |
1225576 KB |
Output is correct |
48 |
Correct |
669 ms |
1225400 KB |
Output is correct |
49 |
Correct |
554 ms |
1225020 KB |
Output is correct |
50 |
Correct |
566 ms |
1225068 KB |
Output is correct |
51 |
Correct |
719 ms |
1225812 KB |
Output is correct |
52 |
Correct |
707 ms |
1225608 KB |
Output is correct |
53 |
Correct |
658 ms |
1225216 KB |
Output is correct |
54 |
Incorrect |
419 ms |
1128936 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |