This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
}
// for(int i = 1; i <= m; i ++){
// for(int j = 1; j <= n; j ++) cout << a[i][j] << " ";
// cout << "\n";
// }
priority_queue<tp, vector<tp>, greater<tp>> q;
f[xs][ys] = 0;
q.push({0, xs, ys});
int ans = oo;
while(!q.empty()){
int val, x, y; tie(val, x, y) = q.top();
// int x = q.front().fi;
// int y = q.front().se;
// cerr << x << " " << y << " y\n";
q.pop();
if(val > f[x][y]) continue;
if(x == xt && y == yt) ans = min(ans, f[x][y]);
// cerr << x << " " << y << " " << f[x][y] << " " << val << " ff\n";
// cerr << pc[5][1] << " uuu\n";
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({f[u][v], 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, g = b;
while(ph[lx][g] >= a){
g = ph[lx][g];
// if(x == 4 && y == 4) cerr << lx << " " << g << " " << f[lx][g] << " t\n";
if(f[lx][g] > f[x][y] + 1) f[lx][g] = f[x][y] + 1, q.push({f[lx][g], lx, g});
if(!ph[lx][g]) break;
ph[lx][g]--;
ph[lx][g] = row(lx, ph[lx][g]);
}
}
if(r + 1 <= m){
int lx = r + 1, g = b;
while(ph[lx][g] >= a){
g = ph[lx][g];
// if(x == 4 && y == 4) cerr << lx << " " << g << " " << f[lx][g] << " t\n";
if(f[lx][g] > f[x][y] + 1) f[lx][g] = f[x][y] + 1, q.push({f[lx][g], lx, g});
if(!ph[lx][g]) break;
ph[lx][g]--;
ph[lx][g] = row(lx, ph[lx][g]);
}
}
if(a - 1 >= 1){
int g = r, ly = a - 1;
while(pc[g][ly] >= l){
g = pc[g][ly];
// if(x == 4 && y == 4) cerr << g << " " << ly << " " << f[g][ly] << " " << pc[g][ly] << " t\n";
if(f[g][ly] > f[x][y] + 1) f[g][ly] = f[x][y] + 1, q.push({f[g][ly], g, ly});
if(!pc[g][ly]) break;
pc[g][ly]--;
pc[g][ly] = col(pc[g][ly], ly);
}
}
if(b + 1 <= n){
int g = r, ly = b + 1;
while(pc[g][ly] >= l){
g = pc[g][ly];
if(f[g][ly] > f[x][y] + 1) f[g][ly] = f[x][y] + 1, q.push({f[g][ly], g, ly});
if(!pc[g][ly]) break;
pc[g][ly]--;
pc[g][ly] = col(pc[g][ly], ly);
}
}
}
cout << min(ans, f[xt][yt]);
}
/*
6 6 3
5 4
2 1
###.#.
#..#..
...###
###.##
..####
#.#...
*/
Compilation message (stderr)
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |