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 = 3001*3001;
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++;
            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],0};
                vis[x[0]][x[1]][2] = 1;
            }
            if(x[2] != k - 1){
                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};
                    }
                }
            }else{
                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] != 2){
                        vis[nx][ny][0] = 1;
                        q[sz++] = {nx,ny};
                    }
                }
            }
        }
        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 - 1){
                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};
                    }
                }
            }else{
                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] != 2){
                        vis[nx][ny][0] = 1;
                        q[sz++] = {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... |