This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
int n, m, k;
vector<vector<int> > a, dp, us;
struct node{
int x=1e9;
node *l=nullptr, *r=nullptr;
node(){}
};
void dupd(node *v, int l, int r, int pos, int x){
if(l == r){
v -> x = min(x, v -> x);
return;
}
int mid=(l+r)/2;
if(mid >= pos){
if(!v -> l) v -> l = new node();
dupd(v -> l, l, mid, pos, x);
}
else{
if(!v -> r) v -> r = new node();
dupd(v -> r, mid + 1, r, pos, x);
}
v -> x = min((v->l ?v -> l -> x :(int)1e9), (v -> r ? v->r->x :(int)1e9));
}
int dget(node *v, int l, int r, int L, int R){
if(v == nullptr) return 1e9;
if(r < L or R < l){
return 1e9;
}
if(L <= l and r <= R){
return v -> x;
}
int mid=(l+r)/2;
return min(dget(v -> l, l, mid, L, R), dget(v -> r, mid+1, r, L, R));
}
vector<node*> t;
void upd(int v, int l, int r, int L, int R, int x){
dupd(t[v], 1, m, R, x);
if(l == r){
return;
}
int mid = (l + r)/2;
if(mid >= L){
upd(v+v, l, mid, L, R, x);
}
else{
upd(v+v+1, mid+1, r, L, R, x);
}
}
int get(int v, int l, int r, int lx, int rx, int ly, int ry){
if(rx < l or r < lx){
return 1e9;
}
if(lx <= l and r <= rx){
return dget(t[v], 1, m, ly, ry);
}
int mid=(l+r)/2;
return min(get(v+v, l, mid, lx, rx, ly, ry), get(v+v+1, mid+1, r, lx, rx, ly, ry));
}
void solve(){
cin>>n>>m>>k;
pii s, g;
cin>>s.ff>>s.ss;
cin>>g.ff>>g.ss;
for(int i=0; i<=4 * n+100; i++){
t.pb(new node());
}
for(int i=0; i<=n + 2; i++){
us.pb({});
dp.pb({});
a.pb({});
for(int j=0; j<=m + 2; j++){
dp[i].pb(0);
us[i].pb(0);
a[i].pb(0);
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
char c;
cin>>c;
if(c == '#') a[i][j] = 1;
else a[i][j] = 0;
}
}
vector<pii> nap = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
deque<pair<pii, int>> h;
h.pb({s, 0});
while(h.size()){
int x=h[0].ff.ff, y=h[0].ff.ss;
if(us[x][y]){
h.pop_front();
continue;
}
dp[x][y] = h[0].ss;
us[x][y] = 1;
upd(1, 1, n, x, y, dp[x][y]);
h.pop_front();
for(pii d: nap){
if(!us[x + d.ff][y + d.ss] and x + d.ff <= n and x + d.ff >= 1 and y + d.ss <= m and y + d.ss >= 1){
if(a[x + d.ff][y + d.ss] == 0) h.push_front({{x + d.ff, y + d.ss}, dp[x][y]});
else{
int g = get(1, 1, n, max(1, x+d.ff-k), min(n, x+d.ff+k), max(1, y+d.ss-k+1), min(m, y+d.ss+k-1));
g = min(g, get(1, 1, n, max(1, x+d.ff-k+1), min(n, x+d.ff+k-1), max(1, y+d.ss-k), min(m, y+d.ss+k)));
if(g < dp[x][y]) h.push_front({{x + d.ff, y + d.ss}, g + 1});
else h.pb({{x + d.ff, y + d.ss}, g + 1});
//cout<<x<<' '<<y<<' '<<x+d.ff<<' '<<y + d.ss<<' '<<max(1, x+d.ff-k)<<' '<<min(n, x+d.ff+k)<<' '<<max(1, y+d.ss-k+1)<<' '<<min(m, y+d.ss+k-1)<<' '<<'\n';
}
continue;
}
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
//cout<<dp[i][j]<<' ';
}
//cout<<'\n';
}
cout<<dp[g.ff][g.ss];
}
int main(){
solve();
}
# | 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... |