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;
const int N = 6e6;
const int inf = 2e9;
vector <vector<char>> v;
vector <vector<bool>> vis[N];
int k,n,m,sx,sy,ex,ey;
bool inbound(int x,int y){
return 0 <= x && x < n && 0 <= y && y < m;
}
queue <array<int,2>> q[2];
queue <array<int,3>> q2;
int d[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
int main(){
int ans = inf;
cin>>n>>m>>k;
cin>>sx>>sy>>ex>>ey;
sx--;sy--;ex--;ey--;
v.assign(n,vector<char>(m,0));
for(int i = 0;i < n;i++){
vis[i].assign(m,vector<bool>(2,0));
for(int j = 0;j < m;j++){
cin>>v[i][j];
}
}
vis[sx][sy][0] = 1;
q[0].push({sx,sy});
for(int i = 0;i <= n + m;i++){
while(!q[i%2].empty()){
auto x = q[i%2].front();
q[i%2].pop();
if(x[0] == ex && x[1] == ey){
ans = i;
}
///bfs
if(!vis[x[0]][x[1]][1]){
q2.push({x[0],x[1],0});
}
for(int j = 0;j < 4;j++){
int nx = x[0] + d[j][0];
int ny = x[1] + d[j][1];
if(inbound(nx,ny) && !vis[nx][ny][0] && v[nx][ny] == '.'){
vis[nx][ny][0] = 1;
q[i%2].push({nx,ny});
}
}
}
while(!q2.empty()){
auto x = q2.front();
q2.pop();
if(!vis[x[0]][x[1]][0]){
q[(i%2)^1].push({x[0],x[1]});
vis[x[0]][x[1]][0] = 1;
}
if(x[2] != k - 1){
for(int j = 0;j < 8;j++){
int nx = x[0] + d[j][0];
int ny = x[1] + d[j][1];
if(inbound(nx,ny) && !vis[nx][ny][1]){
vis[nx][ny][1] = 1;
q2.push({nx,ny,x[2] + 1});
}
}
}else{
for(int j = 0;j < 4;j++){
int nx = x[0] + d[j][0];
int ny = x[1] + d[j][1];
if(inbound(nx,ny) && !vis[nx][ny][0]){
vis[nx][ny][0] = 1;
q[(i%2)^1].push({nx,ny});
}
}
}
}
}
cout<<ans<<'\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... |