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;
}
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;
}
}
deque<pii> DBFS;
dis[s.first][s.second]=0;
DBFS.push_front(s);
while (!DBFS.empty()) {
while (!DBFS.empty() and vis[DBFS.front().first][DBFS.front().second]) DBFS.pop_front();
if (DBFS.empty()) break;
auto [vx,vy]=DBFS.front();
// cout<<vx<<" "<<vy<<"\n"<<flush;
DBFS.pop_front();
vis[vx][vy]=true;
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.emplace_front(nx,ny);
dis[nx][ny]=dis[vx][vy];
}
}
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.emplace_back(g);
}
continue;
}
for (int k=1;k<2*n;k++) {
{ // up
int nx=vx-n;
int ny=vy-n+k;
// cout<<nx<<" "<<ny<<" nn\n";
if (in(nx,ny) and dis[nx][ny]>dis[vx][vy]+1) {
DBFS.emplace_back(nx,ny);
dis[nx][ny]=dis[vx][vy]+1;
}
}
{ // right
int nx=vx-n+k;
int ny=vy+n;
// cout<<nx<<" "<<ny<<" nn\n";
if (in(nx,ny) and dis[nx][ny]>dis[vx][vy]+1) {
DBFS.emplace_back(nx,ny);
dis[nx][ny]=dis[vx][vy]+1;
}
}
{ // down
int nx=vx+n;
int ny=vy-n+k;
// cout<<nx<<" "<<ny<<" nn\n";
if (in(nx,ny) and dis[nx][ny]>dis[vx][vy]+1) {
DBFS.emplace_back(nx,ny);
dis[nx][ny]=dis[vx][vy]+1;
}
}
{ // left
int nx=vx-n+k;
int ny=vy-n;
// cout<<nx<<" "<<ny<<" nn\n";
if (in(nx,ny) and dis[nx][ny]>dis[vx][vy]+1) {
DBFS.emplace_back(nx,ny);
dis[nx][ny]=dis[vx][vy]+1;
}
}
}
}
// 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... |