#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<int,int> pii;
typedef tuple<ll,int,int> tp;
const int N = 6e6 + 5;
const ll oo = 1e14 + 1;
const int mod = 1e9 + 7;
//const ll oo = 5e18;
const int ha[4] = {1, -1, 0, 0};
const int co[4] = {0, 0, 1, -1};
const int tx[4] = {1, 1, -1, -1};
const int ty[4] = {1, -1, -1, 1};
int m, n, k, xt, yt, xs, ys;
ll ans;
vector<ll> f[N];
vector<int> a[N], ph[N], pc[N];
deque<tp> q;
bool kt(int x,int y){
return x >= 1 && x <= m && y >= 1 && y <= n;
}
int row(int u,int v){
return (ph[u][v] == v ? v : ph[u][v] = row(u, ph[u][v]));
}
int col(int u,int v){
return (pc[u][v] == u ? u : pc[u][v] = col(pc[u][v], v));
}
void conduct(ll val,int x,int y){
int l = max(1, x - k + 1), r = min(m, x + k - 1), pl = max(1, y - k + 1), pr = min(n, y + k - 1);
if(xt >= l && xt <= r && yt >= pl && yt <= pr) ans = min(ans, val + 1);
if(l >= 1){
int lx = l, g = pr;
while(ph[lx][g] >= pl){
g = ph[lx][g];
if(f[lx][g] > val + 1) f[lx][g] = val + 1, q.push_back({f[lx][g], lx, g});
if(!ph[lx][g]) break;
ph[lx][g]--;
ph[lx][g] = row(lx, ph[lx][g]);
}
}
if(r != l){
int lx = r, g = pr;
while(ph[lx][g] >= pl){
g = ph[lx][g];
if(f[lx][g] > val + 1) f[lx][g] = val + 1, q.push_back({f[lx][g], lx, g});
if(!ph[lx][g]) break;
ph[lx][g]--;
ph[lx][g] = row(lx, ph[lx][g]);
}
}
if(pl >= 1){
int g = r, ly = pl;
while(pc[g][ly] >= l){
g = pc[g][ly];
if(f[g][ly] > val + 1) f[g][ly] = val + 1, q.push_back({f[g][ly], g, ly});
if(!pc[g][ly]) break;
pc[g][ly]--;
pc[g][ly] = col(pc[g][ly], ly);
}
}
if(pr != pl){
int g = r, ly = pr;
while(pc[g][ly] >= l){
g = pc[g][ly];
if(f[g][ly] > val + 1) f[g][ly] = val + 1, q.push_back({f[g][ly], g, ly});
if(!pc[g][ly]) break;
pc[g][ly]--;
pc[g][ly] = col(pc[g][ly], ly);
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> m >> n >> k;
cin >> xs >> ys >> xt >> yt;
for(int i = 1; i <= m; i ++){
string s; cin >> s;
f[i].pb(oo);
a[i].pb(0);
ph[i].pb(0);
pc[i].pb(0);
for(int j = 0; j < n; j ++){
f[i].pb(oo);
pc[i].pb(0);
ph[i].pb(j + 1);
if(s[j] == '.') a[i].pb(0);
else a[i].pb(1);
}
f[i].pb(oo);
a[i].pb(0);
ph[i].pb(0);
pc[i].pb(0);
}
for(int i = 1; i <= n + 1; i ++) pc[0].pb(0);
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
pc[j][i] = j;
}
}
f[xs][ys] = 0;
q.push_back({0, xs, ys});
ans = oo;
while(!q.empty()){
ll val;
int px, py; tie(val, px, py) = q.front();
q.pop_front();
if(val > f[px][py]) continue;
if(px == xt && py == yt) ans = min(ans, f[px][py]);
for(int d = 0; d < 4; d ++){
int x = px + ha[d];
int y = py + co[d];
if(!kt(x, y) || f[x][y] <= f[px][py] + a[x][y]) continue;
f[x][y] = f[px][py] + a[x][y];
if(a[x][y]) q.push_back({f[x][y], x, y});
else q.push_front({f[x][y], x, y});
if(ph[x][y]){
ph[x][y]--;
ph[x][y] = row(x, ph[x][y]);
}
if(pc[x][y]){
pc[x][y]--;
pc[x][y] = col(pc[x][y], y);
}
conduct(val, x, y);
}
}
cout << min(ans, f[xt][yt]);
}
/*
6 6 3
5 4
2 1
###.#.
#..#..
...###
###.##
..####
#.#...
*/
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
564036 KB |
Output is correct |
2 |
Correct |
226 ms |
563796 KB |
Output is correct |
3 |
Correct |
260 ms |
564080 KB |
Output is correct |
4 |
Correct |
226 ms |
564084 KB |
Output is correct |
5 |
Correct |
228 ms |
564044 KB |
Output is correct |
6 |
Correct |
245 ms |
564012 KB |
Output is correct |
7 |
Correct |
220 ms |
564048 KB |
Output is correct |
8 |
Correct |
223 ms |
564212 KB |
Output is correct |
9 |
Correct |
223 ms |
563848 KB |
Output is correct |
10 |
Correct |
222 ms |
563936 KB |
Output is correct |
11 |
Correct |
217 ms |
564032 KB |
Output is correct |
12 |
Correct |
224 ms |
563912 KB |
Output is correct |
13 |
Correct |
218 ms |
563848 KB |
Output is correct |
14 |
Correct |
235 ms |
563800 KB |
Output is correct |
15 |
Correct |
219 ms |
563796 KB |
Output is correct |
16 |
Correct |
242 ms |
563944 KB |
Output is correct |
17 |
Correct |
230 ms |
564068 KB |
Output is correct |
18 |
Correct |
220 ms |
564068 KB |
Output is correct |
19 |
Correct |
224 ms |
565284 KB |
Output is correct |
20 |
Correct |
228 ms |
565764 KB |
Output is correct |
21 |
Correct |
224 ms |
565588 KB |
Output is correct |
22 |
Correct |
224 ms |
565296 KB |
Output is correct |
23 |
Correct |
230 ms |
565196 KB |
Output is correct |
24 |
Correct |
224 ms |
565708 KB |
Output is correct |
25 |
Correct |
225 ms |
565708 KB |
Output is correct |
26 |
Correct |
228 ms |
565228 KB |
Output is correct |
27 |
Correct |
229 ms |
565192 KB |
Output is correct |
28 |
Correct |
229 ms |
565300 KB |
Output is correct |
29 |
Correct |
243 ms |
568152 KB |
Output is correct |
30 |
Correct |
235 ms |
565404 KB |
Output is correct |
31 |
Correct |
232 ms |
569392 KB |
Output is correct |
32 |
Correct |
240 ms |
567948 KB |
Output is correct |
33 |
Correct |
241 ms |
567960 KB |
Output is correct |
34 |
Correct |
228 ms |
568956 KB |
Output is correct |
35 |
Correct |
234 ms |
569016 KB |
Output is correct |
36 |
Correct |
236 ms |
568148 KB |
Output is correct |
37 |
Correct |
232 ms |
567896 KB |
Output is correct |
38 |
Correct |
234 ms |
567976 KB |
Output is correct |
39 |
Correct |
384 ms |
610292 KB |
Output is correct |
40 |
Correct |
238 ms |
568328 KB |
Output is correct |
41 |
Correct |
232 ms |
570120 KB |
Output is correct |
42 |
Correct |
238 ms |
571076 KB |
Output is correct |
43 |
Correct |
238 ms |
570288 KB |
Output is correct |
44 |
Correct |
279 ms |
580636 KB |
Output is correct |
45 |
Correct |
275 ms |
586728 KB |
Output is correct |
46 |
Correct |
327 ms |
619252 KB |
Output is correct |
47 |
Correct |
366 ms |
611452 KB |
Output is correct |
48 |
Correct |
399 ms |
611412 KB |
Output is correct |
49 |
Correct |
307 ms |
610856 KB |
Output is correct |
50 |
Correct |
305 ms |
610852 KB |
Output is correct |
51 |
Correct |
371 ms |
611792 KB |
Output is correct |
52 |
Correct |
372 ms |
611480 KB |
Output is correct |
53 |
Correct |
389 ms |
611308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
224 ms |
563796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
231 ms |
564028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
224 ms |
563796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
224 ms |
563796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
564036 KB |
Output is correct |
2 |
Correct |
226 ms |
563796 KB |
Output is correct |
3 |
Correct |
260 ms |
564080 KB |
Output is correct |
4 |
Correct |
226 ms |
564084 KB |
Output is correct |
5 |
Correct |
228 ms |
564044 KB |
Output is correct |
6 |
Correct |
245 ms |
564012 KB |
Output is correct |
7 |
Correct |
220 ms |
564048 KB |
Output is correct |
8 |
Correct |
223 ms |
564212 KB |
Output is correct |
9 |
Correct |
223 ms |
563848 KB |
Output is correct |
10 |
Correct |
222 ms |
563936 KB |
Output is correct |
11 |
Correct |
217 ms |
564032 KB |
Output is correct |
12 |
Correct |
224 ms |
563912 KB |
Output is correct |
13 |
Correct |
218 ms |
563848 KB |
Output is correct |
14 |
Correct |
235 ms |
563800 KB |
Output is correct |
15 |
Correct |
219 ms |
563796 KB |
Output is correct |
16 |
Correct |
242 ms |
563944 KB |
Output is correct |
17 |
Correct |
230 ms |
564068 KB |
Output is correct |
18 |
Correct |
220 ms |
564068 KB |
Output is correct |
19 |
Correct |
224 ms |
565284 KB |
Output is correct |
20 |
Correct |
228 ms |
565764 KB |
Output is correct |
21 |
Correct |
224 ms |
565588 KB |
Output is correct |
22 |
Correct |
224 ms |
565296 KB |
Output is correct |
23 |
Correct |
230 ms |
565196 KB |
Output is correct |
24 |
Correct |
224 ms |
565708 KB |
Output is correct |
25 |
Correct |
225 ms |
565708 KB |
Output is correct |
26 |
Correct |
228 ms |
565228 KB |
Output is correct |
27 |
Correct |
229 ms |
565192 KB |
Output is correct |
28 |
Correct |
229 ms |
565300 KB |
Output is correct |
29 |
Correct |
243 ms |
568152 KB |
Output is correct |
30 |
Correct |
235 ms |
565404 KB |
Output is correct |
31 |
Correct |
232 ms |
569392 KB |
Output is correct |
32 |
Correct |
240 ms |
567948 KB |
Output is correct |
33 |
Correct |
241 ms |
567960 KB |
Output is correct |
34 |
Correct |
228 ms |
568956 KB |
Output is correct |
35 |
Correct |
234 ms |
569016 KB |
Output is correct |
36 |
Correct |
236 ms |
568148 KB |
Output is correct |
37 |
Correct |
232 ms |
567896 KB |
Output is correct |
38 |
Correct |
234 ms |
567976 KB |
Output is correct |
39 |
Correct |
384 ms |
610292 KB |
Output is correct |
40 |
Correct |
238 ms |
568328 KB |
Output is correct |
41 |
Correct |
232 ms |
570120 KB |
Output is correct |
42 |
Correct |
238 ms |
571076 KB |
Output is correct |
43 |
Correct |
238 ms |
570288 KB |
Output is correct |
44 |
Correct |
279 ms |
580636 KB |
Output is correct |
45 |
Correct |
275 ms |
586728 KB |
Output is correct |
46 |
Correct |
327 ms |
619252 KB |
Output is correct |
47 |
Correct |
366 ms |
611452 KB |
Output is correct |
48 |
Correct |
399 ms |
611412 KB |
Output is correct |
49 |
Correct |
307 ms |
610856 KB |
Output is correct |
50 |
Correct |
305 ms |
610852 KB |
Output is correct |
51 |
Correct |
371 ms |
611792 KB |
Output is correct |
52 |
Correct |
372 ms |
611480 KB |
Output is correct |
53 |
Correct |
389 ms |
611308 KB |
Output is correct |
54 |
Incorrect |
224 ms |
563796 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
564036 KB |
Output is correct |
2 |
Correct |
226 ms |
563796 KB |
Output is correct |
3 |
Correct |
260 ms |
564080 KB |
Output is correct |
4 |
Correct |
226 ms |
564084 KB |
Output is correct |
5 |
Correct |
228 ms |
564044 KB |
Output is correct |
6 |
Correct |
245 ms |
564012 KB |
Output is correct |
7 |
Correct |
220 ms |
564048 KB |
Output is correct |
8 |
Correct |
223 ms |
564212 KB |
Output is correct |
9 |
Correct |
223 ms |
563848 KB |
Output is correct |
10 |
Correct |
222 ms |
563936 KB |
Output is correct |
11 |
Correct |
217 ms |
564032 KB |
Output is correct |
12 |
Correct |
224 ms |
563912 KB |
Output is correct |
13 |
Correct |
218 ms |
563848 KB |
Output is correct |
14 |
Correct |
235 ms |
563800 KB |
Output is correct |
15 |
Correct |
219 ms |
563796 KB |
Output is correct |
16 |
Correct |
242 ms |
563944 KB |
Output is correct |
17 |
Correct |
230 ms |
564068 KB |
Output is correct |
18 |
Correct |
220 ms |
564068 KB |
Output is correct |
19 |
Correct |
224 ms |
565284 KB |
Output is correct |
20 |
Correct |
228 ms |
565764 KB |
Output is correct |
21 |
Correct |
224 ms |
565588 KB |
Output is correct |
22 |
Correct |
224 ms |
565296 KB |
Output is correct |
23 |
Correct |
230 ms |
565196 KB |
Output is correct |
24 |
Correct |
224 ms |
565708 KB |
Output is correct |
25 |
Correct |
225 ms |
565708 KB |
Output is correct |
26 |
Correct |
228 ms |
565228 KB |
Output is correct |
27 |
Correct |
229 ms |
565192 KB |
Output is correct |
28 |
Correct |
229 ms |
565300 KB |
Output is correct |
29 |
Correct |
243 ms |
568152 KB |
Output is correct |
30 |
Correct |
235 ms |
565404 KB |
Output is correct |
31 |
Correct |
232 ms |
569392 KB |
Output is correct |
32 |
Correct |
240 ms |
567948 KB |
Output is correct |
33 |
Correct |
241 ms |
567960 KB |
Output is correct |
34 |
Correct |
228 ms |
568956 KB |
Output is correct |
35 |
Correct |
234 ms |
569016 KB |
Output is correct |
36 |
Correct |
236 ms |
568148 KB |
Output is correct |
37 |
Correct |
232 ms |
567896 KB |
Output is correct |
38 |
Correct |
234 ms |
567976 KB |
Output is correct |
39 |
Correct |
384 ms |
610292 KB |
Output is correct |
40 |
Correct |
238 ms |
568328 KB |
Output is correct |
41 |
Correct |
232 ms |
570120 KB |
Output is correct |
42 |
Correct |
238 ms |
571076 KB |
Output is correct |
43 |
Correct |
238 ms |
570288 KB |
Output is correct |
44 |
Correct |
279 ms |
580636 KB |
Output is correct |
45 |
Correct |
275 ms |
586728 KB |
Output is correct |
46 |
Correct |
327 ms |
619252 KB |
Output is correct |
47 |
Correct |
366 ms |
611452 KB |
Output is correct |
48 |
Correct |
399 ms |
611412 KB |
Output is correct |
49 |
Correct |
307 ms |
610856 KB |
Output is correct |
50 |
Correct |
305 ms |
610852 KB |
Output is correct |
51 |
Correct |
371 ms |
611792 KB |
Output is correct |
52 |
Correct |
372 ms |
611480 KB |
Output is correct |
53 |
Correct |
389 ms |
611308 KB |
Output is correct |
54 |
Incorrect |
224 ms |
563796 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
564036 KB |
Output is correct |
2 |
Correct |
226 ms |
563796 KB |
Output is correct |
3 |
Correct |
260 ms |
564080 KB |
Output is correct |
4 |
Correct |
226 ms |
564084 KB |
Output is correct |
5 |
Correct |
228 ms |
564044 KB |
Output is correct |
6 |
Correct |
245 ms |
564012 KB |
Output is correct |
7 |
Correct |
220 ms |
564048 KB |
Output is correct |
8 |
Correct |
223 ms |
564212 KB |
Output is correct |
9 |
Correct |
223 ms |
563848 KB |
Output is correct |
10 |
Correct |
222 ms |
563936 KB |
Output is correct |
11 |
Correct |
217 ms |
564032 KB |
Output is correct |
12 |
Correct |
224 ms |
563912 KB |
Output is correct |
13 |
Correct |
218 ms |
563848 KB |
Output is correct |
14 |
Correct |
235 ms |
563800 KB |
Output is correct |
15 |
Correct |
219 ms |
563796 KB |
Output is correct |
16 |
Correct |
242 ms |
563944 KB |
Output is correct |
17 |
Correct |
230 ms |
564068 KB |
Output is correct |
18 |
Correct |
220 ms |
564068 KB |
Output is correct |
19 |
Correct |
224 ms |
565284 KB |
Output is correct |
20 |
Correct |
228 ms |
565764 KB |
Output is correct |
21 |
Correct |
224 ms |
565588 KB |
Output is correct |
22 |
Correct |
224 ms |
565296 KB |
Output is correct |
23 |
Correct |
230 ms |
565196 KB |
Output is correct |
24 |
Correct |
224 ms |
565708 KB |
Output is correct |
25 |
Correct |
225 ms |
565708 KB |
Output is correct |
26 |
Correct |
228 ms |
565228 KB |
Output is correct |
27 |
Correct |
229 ms |
565192 KB |
Output is correct |
28 |
Correct |
229 ms |
565300 KB |
Output is correct |
29 |
Correct |
243 ms |
568152 KB |
Output is correct |
30 |
Correct |
235 ms |
565404 KB |
Output is correct |
31 |
Correct |
232 ms |
569392 KB |
Output is correct |
32 |
Correct |
240 ms |
567948 KB |
Output is correct |
33 |
Correct |
241 ms |
567960 KB |
Output is correct |
34 |
Correct |
228 ms |
568956 KB |
Output is correct |
35 |
Correct |
234 ms |
569016 KB |
Output is correct |
36 |
Correct |
236 ms |
568148 KB |
Output is correct |
37 |
Correct |
232 ms |
567896 KB |
Output is correct |
38 |
Correct |
234 ms |
567976 KB |
Output is correct |
39 |
Correct |
384 ms |
610292 KB |
Output is correct |
40 |
Correct |
238 ms |
568328 KB |
Output is correct |
41 |
Correct |
232 ms |
570120 KB |
Output is correct |
42 |
Correct |
238 ms |
571076 KB |
Output is correct |
43 |
Correct |
238 ms |
570288 KB |
Output is correct |
44 |
Correct |
279 ms |
580636 KB |
Output is correct |
45 |
Correct |
275 ms |
586728 KB |
Output is correct |
46 |
Correct |
327 ms |
619252 KB |
Output is correct |
47 |
Correct |
366 ms |
611452 KB |
Output is correct |
48 |
Correct |
399 ms |
611412 KB |
Output is correct |
49 |
Correct |
307 ms |
610856 KB |
Output is correct |
50 |
Correct |
305 ms |
610852 KB |
Output is correct |
51 |
Correct |
371 ms |
611792 KB |
Output is correct |
52 |
Correct |
372 ms |
611480 KB |
Output is correct |
53 |
Correct |
389 ms |
611308 KB |
Output is correct |
54 |
Incorrect |
224 ms |
563796 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |