//#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<int,int,int> tp;
const int N = 3e6 + 5;
const int oo = 1e9 + 1;
const int mod = 1e9 + 7;
int block = 316;
//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;
vector<int> f[N], a[N], ph[N], pc[N];
bool kt(int x,int y){
return x >= 1 && x <= m && y >= 1 && y <= n;
}
int row(int u,int v){
// cerr << u << " " << v << " t\n";
return (ph[u][v] == v ? v : ph[u][v] = row(u, ph[u][v]));
}
int col(int u,int v){
// cerr << u << " " << v << " " << pc[u][v] << " w\n";
// if(pc[u][v] == u) return u;
return (pc[u][v] == u ? u : pc[u][v] = col(pc[u][v], v));
}
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;
}
}
queue<pii> q;
f[xs][ys] = 0;
q.push({xs, ys});
int ans = oo;
while(!q.empty()){
int x = q.front().fi;
int y = q.front().se;
// cerr << x << " " << y << " y\n";
q.pop();
if(x == xt && y == yt){
cout << min(ans, f[x][y]);
return 0;
}
for(int d = 0; d < 4; d ++){
int u = x + ha[d];
int v = y + co[d];
// cerr << u << " " << v << " t\n";
if(!kt(u, v) || f[u][v] <= f[x][y] + a[u][v]) continue;
f[u][v] = f[x][y] + a[u][v];
q.push({u, v});
if(ph[u][v]){
ph[u][v]--;
ph[u][v] = row(u, ph[u][v]);
}
if(pc[u][v]){
pc[u][v]--;
pc[u][v] = col(pc[u][v], v);
}
}
int l = max(1, x - k + 1), r = min(m, x + k - 1), a = max(1, y - k + 1), b = min(n, y + k - 1);
if(xt >= l && xt <= r && yt >= a && yt <= b) ans = min(ans, f[x][y] + 1);
// for(int d = 0; d < 4; d ++){
// int u = x + d * (k - 1); u = max(u, 1), u = min(u, m);
// int v = y + d * (k - 1); v = max(v, 1), v = min(v, n);
// if(f[u][v] > f[x][y] + 1){
// q.push({u, v});
// f[u][v] = f[x][y] + 1;
// ph[u][v] = le[u][v];
// ph[u][v] = row(u, ph[u][v]);
// pc[u][v] = up[u][v];
// pc[u][v] = col(pc[u][v], v);
// }
// }
// cerr << " done\n";
if(l - 1 >= 1){
int lx = l - 1, pos = b;
while(ph[lx][pos] >= a){
int g = ph[lx][pos];
if(f[lx][g] > f[x][y] + 1) f[lx][g] = f[x][y] + 1, q.push({lx, g});
if(!ph[lx][pos]) break;
ph[lx][pos]--;
ph[lx][pos] = row(lx, ph[lx][pos]);
}
}
if(r + 1 <= m){
int lx = r + 1, pos = b;
while(ph[lx][pos] >= a){
int g = ph[lx][pos];
if(f[lx][g] > f[x][y] + 1) f[lx][g] = f[x][y] + 1, q.push({lx, g});
if(!ph[lx][pos]) break;
ph[lx][pos]--;
ph[lx][pos] = row(lx, ph[lx][pos]);
}
}
if(a - 1 >= 1){
int pos = r, ly = a - 1;
while(pc[pos][ly] >= l){
int g = pc[pos][ly];
if(f[g][ly] > f[x][y] + 1) f[g][ly] = f[x][y] + 1, q.push({g, ly});
if(!pc[g][ly]) break;
pc[g][ly]--;
pc[g][ly] = col(pc[g][ly], ly);
}
}
if(b + 1 <= n){
int pos = r, ly = b + 1;
while(pc[pos][ly] >= l){
int g = pc[pos][ly];
if(f[g][ly] > f[x][y] + 1) f[g][ly] = f[x][y] + 1, q.push({g, ly});
if(!pc[g][ly]) break;
pc[g][ly]--;
pc[g][ly] = col(pc[g][ly], ly);
}
}
}
cout << min(ans, f[m][n]);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
282204 KB |
Output is correct |
2 |
Correct |
72 ms |
282192 KB |
Output is correct |
3 |
Correct |
70 ms |
282192 KB |
Output is correct |
4 |
Correct |
72 ms |
282192 KB |
Output is correct |
5 |
Incorrect |
71 ms |
282456 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
282224 KB |
Output is correct |
2 |
Correct |
70 ms |
282240 KB |
Output is correct |
3 |
Correct |
69 ms |
282240 KB |
Output is correct |
4 |
Correct |
68 ms |
282196 KB |
Output is correct |
5 |
Correct |
68 ms |
282192 KB |
Output is correct |
6 |
Incorrect |
73 ms |
282140 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
282196 KB |
Output is correct |
2 |
Correct |
69 ms |
282240 KB |
Output is correct |
3 |
Correct |
68 ms |
282572 KB |
Output is correct |
4 |
Correct |
65 ms |
282244 KB |
Output is correct |
5 |
Incorrect |
68 ms |
282244 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
282224 KB |
Output is correct |
2 |
Correct |
70 ms |
282240 KB |
Output is correct |
3 |
Correct |
69 ms |
282240 KB |
Output is correct |
4 |
Correct |
68 ms |
282196 KB |
Output is correct |
5 |
Correct |
68 ms |
282192 KB |
Output is correct |
6 |
Incorrect |
73 ms |
282140 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
282224 KB |
Output is correct |
2 |
Correct |
70 ms |
282240 KB |
Output is correct |
3 |
Correct |
69 ms |
282240 KB |
Output is correct |
4 |
Correct |
68 ms |
282196 KB |
Output is correct |
5 |
Correct |
68 ms |
282192 KB |
Output is correct |
6 |
Incorrect |
73 ms |
282140 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
282204 KB |
Output is correct |
2 |
Correct |
72 ms |
282192 KB |
Output is correct |
3 |
Correct |
70 ms |
282192 KB |
Output is correct |
4 |
Correct |
72 ms |
282192 KB |
Output is correct |
5 |
Incorrect |
71 ms |
282456 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
282204 KB |
Output is correct |
2 |
Correct |
72 ms |
282192 KB |
Output is correct |
3 |
Correct |
70 ms |
282192 KB |
Output is correct |
4 |
Correct |
72 ms |
282192 KB |
Output is correct |
5 |
Incorrect |
71 ms |
282456 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
282204 KB |
Output is correct |
2 |
Correct |
72 ms |
282192 KB |
Output is correct |
3 |
Correct |
70 ms |
282192 KB |
Output is correct |
4 |
Correct |
72 ms |
282192 KB |
Output is correct |
5 |
Incorrect |
71 ms |
282456 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |