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>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=5e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
int r,c,n;
pii s,g;
vector<vector<bool> > mp,vis;
vector<vector<int> > dis;
bool in(int x,int y) {
return x>=0 and x<r and y>=0 and y<c;
}
bool inrg(int vx,int vy,int tgx,int tgy) {
int dx=abs(tgx-vx);
int dy=abs(tgy-vy);
if (max(dx,dy)<=n and !(dx==n and dy==n)) return true;
return false;
}
vector<vector<int> > rw;
int rwnxt(int i,int j) {
if (j==c or !vis[i][j]) return j;
return rw[i][j]=rwnxt(i,rw[i][j]);
}
vector<vector<int> > col;
int colnxt(int i,int j) {
if (i==r or !vis[i][j]) return i;
return col[i][j]=colnxt(col[i][j],j);
}
int main() {
speed;
cin>>r>>c>>n;
cin>>s.first>>s.second;
s.first--;s.second--;
cin>>g.first>>g.second;
g.first--;g.second--;
mp.resize(r,vector<bool>(c));
dis.resize(r,vector<int>(c,INF));
vis.resize(r,vector<bool>(c,0));
for (int i=0;i<r;i++) {
for (int j=0;j<c;j++) {
char c;
cin>>c;
if (c=='.') mp[i][j]=0;
else mp[i][j]=1;
}
}
rw.resize(r,vector<int>(c));
col.resize(r,vector<int>(c));
for (int i=0;i<r;i++) {
for (int j=0;j<c;j++) {
rw[i][j]=j+1;
col[i][j]=i+1;
}
}
vector<pii> cur0,cur1;
cur0.emplace_back(s);
vis[s.first][s.second]=true;
dis[s.first][s.second]=0;
while (!cur0.empty()) {
while (!cur0.empty()) {
auto [vx,vy]=cur0.back();
cur1.emplace_back(vx,vy);
cur0.pop_back();
for (int k=0;k<4;k++) {
int nx=vx+dir[k][0];
int ny=vy+dir[k][1];
if (in(nx,ny) and !mp[nx][ny] and !vis[nx][ny]) {
dis[nx][ny]=dis[vx][vy];
vis[nx][ny]=true;
cur0.emplace_back(nx,ny);
// cout<<nx<<" "<<ny<<"\n";
if (vis[g.first][g.second]) goto found;
}
}
}
while (!cur1.empty()) {
auto [vx,vy]=cur1.back();
cur1.pop_back();
// cout<<vx<<" "<<vy<<"\n"<<flush;
if (inrg(vx,vy,g.first,g.second)) {
dis[g.first][g.second]=dis[vx][vy]+1;
goto found;
}
for (int nx:{max(0,vx-n),min(r-1,vx+n)}) {
// cout<<nx<<" "<<max(0,vy-n+1)<<" rw\n"<<flush;
int ny=rwnxt(nx,max(0,vy-n+1));
while(ny<min(c,vy+n)) {
// cout<<nx<<" "<<ny<<" rwn\n"<<flush;
dis[nx][ny]=dis[vx][vy]+1;
vis[nx][ny]=true;
cur0.emplace_back(nx,ny);
ny=rwnxt(nx,ny);
}
}
for (int ny:{max(0,vy-n),min(c-1,vy+n)}) {
int nx=colnxt(max(0,vx-n+1),ny);
while(nx<min(r,vx+n)) {
// cout<<nx<<" "<<ny<<" coln\n";
dis[nx][ny]=dis[vx][vy]+1;
vis[nx][ny]=true;
cur0.emplace_back(nx,ny);
nx=colnxt(nx,ny);
}
}
}
// cout<<"---------\n";
}
found:
// for (int i=0;i<r;i++) {
// for (int j=0;j<c;j++) cout<<(dis[i][j]==INF?0:dis[i][j])<<" ";
// cout<<"\n";
// }
cout<<dis[g.first][g.second]<<"\n";
return 0;
}
# | 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... |