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<int>> dist;
vector <vector<vector<bool>>> vis;
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(){
cin>>n>>m>>k;
cin>>sx>>sy>>ex>>ey;
sx--;sy--;ex--;ey--;
dist.assign(n,vector<int>(m,inf));
v.assign(n,vector<char>(m,0));
vis.assign(n,vector<vector<bool>>(m,vector<bool>({0,0})));
for(int i = 0;i < n;i++){
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();
dist[x[0]][x[1]] = i;
//cout<<x[0]<<' '<<x[1]<<' '<<i<<'\n';
q[i%2].pop();
///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<<nx<<' '<<ny<<"whatthefuckiswrong\n";
}
}
}
}
cout<<dist[ex][ey];
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... |