#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef pair<ll,ll> pill;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF (ll)1e18
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
typedef pair <pi,pi> pipi;
#define maxn 500010
int N,M,K,sr,sc,gr,gc;
struct node{
int s,e,m;
node *l,*r;
set <int> st;
node(int ss,int ee){
s = ss; e = ee; m = (s + e) / 2;
if (s != e){
l = new node(s,m); r = new node(m+1,e);
}
FOR(i,1,M+1) st.insert(i);
}
void upd(int p,int v){
if (s == e){
st.erase(v); return;
}
if (p > m) r->upd(p,v);
else l->upd(p,v);
if (l->st.find(v) == l->st.end() && r->st.find(v) == r->st.end()) st.erase(v);
}
int qry(int a,int b,int c){
if (a <= s && e <= b){
return *st.lower_bound(c);
}else if (a > e || s > b) return M+1;
else return min(l->qry(a,b,c), r->qry(a,b,c));
}
}*root;
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};
int32_t main(){
fast;
cin >> N >> M >> K;
cin >> sr >> sc >> gr >> gc;
bool swapped = 0;
if (M > N){
swap(M,N);
swap(sr,sc); swap(gr,gc);
swapped = 1;
}
bool A[N+1][M+1];
set <int> vis[M+1];
root = new node(1,N);
char ch;
if (swapped){
FOR(i,1,M) FOR(j,1,N){
cin >> ch; A[j][i] = (ch == '.');
}
}else{
FOR(i,1,N) FOR(j,1,M){
cin >> ch; A[i][j] = (ch == '.');
}
}
//~ cout << N << ' ' << M << '\n';
FOR(i,1,N) FOR(j,1,M) vis[j].insert(i);
deque <pii> dq;
dq.push_front(pii(pi(sr,sc), 0));
root->upd(sr,sc);
while (!dq.empty()){
pii cur = dq.front(); dq.pop_front();
int x = cur.f.f, y = cur.f.s, d = cur.s;
if (vis[y].find(x) == vis[y].end()) continue;
vis[y].erase(x);
//~ cout << x << ' ' << y << '\n';
if (x == gr && y == gc){
cout << d << '\n'; return 0;
}
FOR(i,0,3){
int nx = x + dx[i], ny = y + dy[i];
if (nx > 0 && ny > 0 && nx <= N && ny <= M && A[nx][ny] && vis[ny].find(nx) != vis[ny].end()){
root->upd(nx,ny);
dq.push_front(pii(pi(nx,ny), d));
}
}
vpi v;
bool done = 0;
while (!done){
done = 1;
int ny = root->qry(max(1LL, x-K+1),min(N, x+K-1),y-K);
if (ny <= min(M, y+K)){
int nx = *vis[ny].lower_bound(x-K+1);
root->upd(nx,ny);
v.pb(pi(nx,ny));
//~ cout << nx << ' ' << ny << '\n';
vis[ny].erase(nx);
dq.push_back(pii(pi(nx,ny), d+1));
done = 0;
}
ny = root->qry(max(1LL, x-K),min(N, x+K),y-K+1);
if (ny <= min(M, y+K-1)){
int nx = *vis[ny].lower_bound(x-K);
root->upd(nx,ny);
v.pb(pi(nx,ny));
vis[ny].erase(nx);
dq.push_back(pii(pi(nx,ny), d+1));
done = 0;
}
}
aFOR(i, v) vis[i.s].insert(i.f);
}
}
# | 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... |