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<set<int> > rw,col;
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;
}
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);
col.resize(c);
for (int i=0;i<r;i++) {
for (int j=0;j<c;j++) {
rw[i].insert(j);
col[j].insert(i);
}
}
deque<pii> DBFS;
dis[s.first][s.second]=0;
DBFS.push_front(s);
while (!DBFS.empty()) {
auto [vx,vy]=DBFS.front();
// cout<<vx<<" "<<vy<<" v\n"<<flush;
DBFS.pop_front();
for (int k=0;k<4;k++) {
int nx=vx+dir[k][0];
int ny=vy+dir[k][1];
// cout<<nx<<" "<<ny<<" n\n";
// if (in(nx,ny)) cout<<dis[nx][ny]<<" "<<dis[vx][vy]<<" "<<mp[nx][ny]<<"\n";
if (in(nx,ny) and !mp[nx][ny] and dis[nx][ny]>dis[vx][vy]) {
DBFS.push_front({nx,ny});
dis[nx][ny]=dis[vx][vy];
rw[nx].erase(ny);
col[ny].erase(nx);
}
}
if (inrg(vx,vy,g.first,g.second)) {
if (dis[g.first][g.second]>dis[vx][vy]+1) {
dis[g.first][g.second]=dis[vx][vy]+1;
DBFS.push_back(g);
}
continue;
}
if (vx-n>=0) {
int nx=vx-n;
auto it=rw[nx].upper_bound(max(0,vy-n+1)-1);
while (it!=rw[nx].end() and *it<min(c,vy+n)) { // up
// cout<<nx<<" "<<ny<<" nU\n";
if (dis[nx][*it]>dis[vx][vy]+1) {
DBFS.push_back({nx,*it});
dis[nx][*it]=dis[vx][vy]+1;
}
col[*it].erase(nx);
it=rw[nx].erase(it);
}
}
if (vx+n<r) {
int nx=vx+n;
auto it=rw[nx].upper_bound(max(0,vy-n+1)-1);
while (it!=rw[nx].end() and *it<min(c,vy+n)) { // down
// cout<<nx<<" "<<ny<<" nD\n";
if (dis[nx][*it]>dis[vx][vy]+1) {
DBFS.push_back({nx,*it});
dis[nx][*it]=dis[vx][vy]+1;
}
col[*it].erase(nx);
it=rw[nx].erase(it);
}
}
if (vy+n<c) {
int ny=vy+n;
auto it=col[ny].upper_bound(max(0,vx-n+1)-1);
while (it!=col[ny].end() and *it<min(r,vx+n)) { // right
// cout<<nx<<" "<<ny<<" nR\n";
if (dis[*it][ny]>dis[vx][vy]+1) {
DBFS.push_back({*it,ny});
dis[*it][ny]=dis[vx][vy]+1;
}
rw[*it].erase(ny);
it=col[ny].erase(it);
}
}
if (vy-n>=0) {
int ny=vy-n;
auto it=col[ny].upper_bound(max(0,vx-n+1)-1);
while (it!=col[ny].end() and *it<min(r,vx+n)) { // left
// cout<<nx<<" "<<ny<<" nL\n";
if (dis[*it][ny]>dis[vx][vy]+1) {
DBFS.push_back({*it,ny});
dis[*it][ny]=dis[vx][vy]+1;
}
rw[*it].erase(ny);
it=col[ny].erase(it);
}
}
}
// for (int i=0;i<r;i++) {
// for (int j=0;j<c;j++) cout<<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... |