#include<bits/stdc++.h>
using namespace std;
const int N = 6e6;
const int inf = 2e9;
vector <char> v[N + 2];
vector <vector<bool>> vis[N + 2];
int k,n,m,sx,sy,ex,ey;
array<int,2> q[N + 5];
array<int,3> q2[N + 5];
array<int,3> q3[N + 5];
int d[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int ans = inf;
cin>>n>>m>>k;
cin>>sx>>sy>>ex>>ey;
for(int i = 0;i < n + 2;i++){
v[i].assign(m + 2,0);
vis[i].assign(m + 2,vector<bool>(3,0));
v[i][0] = 2;
v[i][m + 1] = 2;
}
for(int i = 0;i < m + 2;i++){
v[0][i] = 2;
v[n + 1][i] = 2;
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
char ch;
cin>>ch;
if(ch == '.'){
v[i + 1][j + 1] = 0;
}else{
v[i + 1][j + 1] = 1;
}
}
}
vis[sx][sy][0] = 1;
int pt = 0,sz = 0;
int pt2 = 0,sz2 = 0;
int pt3 = 0,sz3 = 0;
q[sz++] = {sx,sy};
for(int i = 0;i <= n + m;i++){
while(pt < sz){
auto x = q[pt];pt++;
//cout<<x[0]<<' '<<x[1]<<' '<<i<<'\n';
if(x[0] == ex && x[1] == ey){
ans = i;
}
///bfs
if(!vis[x[0]][x[1]][1]){
q2[sz2++] = {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(!vis[nx][ny][0] && v[nx][ny] == 0){
vis[nx][ny][0] = 1;
q[sz++] = {nx,ny};
}
}
}
while(pt2 < sz2){
auto x = q2[pt2];
pt2++;
if(!vis[x[0]][x[1]][2]){
q3[sz3++] = {x[0],x[1],(x[2] == k?1:0)};
vis[x[0]][x[1]][2] = 1;
}
if(x[2] != k){
for(int j = 0;j < 2;j++){
int nx = x[0] + d[j][0];
int ny = x[1] + d[j][1];
if(!vis[nx][ny][1] && v[nx][ny] != 2){
vis[nx][ny][1] = 1;
q2[sz2++] = {nx,ny,x[2] + 1};
}
}
}
}
while(pt3 < sz3){
auto x = q3[pt3];
pt3++;
if(!vis[x[0]][x[1]][0]){
q[sz++] = {x[0],x[1]};
vis[x[0]][x[1]][0] = 1;
}
if(x[2] != k){
for(int j = 2;j < 4;j++){
int nx = x[0] + d[j][0];
int ny = x[1] + d[j][1];
if(!vis[nx][ny][2] && v[nx][ny] != 2){
vis[nx][ny][2] = 1;
q3[sz3++] = {nx,ny,x[2] + 1};
}
}
}
}
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
282196 KB |
Output is correct |
2 |
Correct |
112 ms |
282028 KB |
Output is correct |
3 |
Incorrect |
112 ms |
282196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
282204 KB |
Output is correct |
2 |
Correct |
113 ms |
282092 KB |
Output is correct |
3 |
Correct |
126 ms |
282192 KB |
Output is correct |
4 |
Correct |
121 ms |
282192 KB |
Output is correct |
5 |
Incorrect |
119 ms |
282192 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
282144 KB |
Output is correct |
2 |
Correct |
119 ms |
282072 KB |
Output is correct |
3 |
Correct |
123 ms |
282196 KB |
Output is correct |
4 |
Correct |
123 ms |
282196 KB |
Output is correct |
5 |
Correct |
125 ms |
282448 KB |
Output is correct |
6 |
Correct |
125 ms |
282452 KB |
Output is correct |
7 |
Correct |
122 ms |
282136 KB |
Output is correct |
8 |
Incorrect |
120 ms |
282280 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
282204 KB |
Output is correct |
2 |
Correct |
113 ms |
282092 KB |
Output is correct |
3 |
Correct |
126 ms |
282192 KB |
Output is correct |
4 |
Correct |
121 ms |
282192 KB |
Output is correct |
5 |
Incorrect |
119 ms |
282192 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
282204 KB |
Output is correct |
2 |
Correct |
113 ms |
282092 KB |
Output is correct |
3 |
Correct |
126 ms |
282192 KB |
Output is correct |
4 |
Correct |
121 ms |
282192 KB |
Output is correct |
5 |
Incorrect |
119 ms |
282192 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
282196 KB |
Output is correct |
2 |
Correct |
112 ms |
282028 KB |
Output is correct |
3 |
Incorrect |
112 ms |
282196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
282196 KB |
Output is correct |
2 |
Correct |
112 ms |
282028 KB |
Output is correct |
3 |
Incorrect |
112 ms |
282196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
282196 KB |
Output is correct |
2 |
Correct |
112 ms |
282028 KB |
Output is correct |
3 |
Incorrect |
112 ms |
282196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |