Problem:
Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

1 \leq H,W,K \leq 10^6

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:

From (3, 2), go west one square to (3, 1).

From (3, 1), go north two squares to (1, 1).

From (1, 1), go east two squares to (1, 3).

From (1, 3), go east one square to (1, 4).

From (1, 4), go south two squares to (3, 4).

Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1

Solution in C++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#include<iostream>
#include<vector>
#include<queue>
#include<climits>
#include<cmath>

typedef std::pair<int,int> index;
typedef std::pair<index,char> indexWithDirect;
typedef std::pair<indexWithDirect,double> priorityNode;

class grid{
public:
    bool isBarier;              // lotus or not
    bool isSearched;            // once searched or not
    long double cost[4];
    grid(){
        isBarier=0;
        isSearched=0;
        for(int i=0;i<4;++i){
            cost[i]=INT_MAX;
        }
    }
};

enum DIRECT{
    EAST,
    WEST,
    SOUTH,
    NORTH,
    NUM_DIRECT,
};

index moveV(index pos,int direct,int distance){
    index movedPos=pos;
    switch (direct){
    case EAST:
        movedPos.second+=distance;
        break;
    case WEST:
        movedPos.second-=distance;
        break;
    case SOUTH:
        movedPos.first+=distance;
        break;
    case NORTH:
        movedPos.first-=distance;
        break;
    default:
        break;
    }
    return movedPos;
}

bool isEnableMove(index movedPos,std::vector<std::vector<grid> > pond){
    if(movedPos.first>=0 && movedPos.second>=0){
        if(movedPos.first<pond.size() && movedPos.second<pond[0].size()){
            if(pond[movedPos.first][movedPos.second].isBarier==0){
                return 1;
            }
        }
    }
    return 0;
}
/*



*/

double calcCost(double preCost,char movingDirect,char movedDirect,int distance,int K){
    double cost=preCost+distance/(double)K;
    if(movingDirect!=movedDirect){
        cost=ceil(cost);
    }
    return cost;
}

void print(index id,char end='\n'){
    std::cout<<"("<<id.first<<","<<id.second<<")"<<end;
}

void print(indexWithDirect iwd,char end='\n'){
    print(iwd.first,' ');
    std::cout<<"direct:"<<iwd.second<<end;
}

void print(priorityNode n,char end='\n'){
    print(n.first,' ');
    std::cout<<"cost:"<<n.second<<end;
}

void print(std::vector<std::vector<grid> >pond){
    for(auto r:pond){
        for(auto g:r){
            double cost=g.cost[0];
            for(int i=1;i<4;++i)
                if(cost>g.cost[i])
                    cost=g.cost[i];
            std::cout<<cost<<"("<<g.isBarier<<")\t";
        }
        std::cout<<std::endl;
    }
}

void dijkstra(){
    int H,W,K;
    std::cin>>H>>W>>K;
    index start,goal;
    std::vector<std::vector<grid>> pond(H,std::vector<grid>(W));
    auto compare=[](priorityNode l,priorityNode r){return l.second>r.second;};
    std::priority_queue<priorityNode,std::vector<priorityNode>,decltype(compare)> q{compare};
    std::cin>>start.first>>start.second;
    std::cin>>goal.first>>goal.second;

    start.first-=1;
    start.second-=1;
    goal.first-=1;
    goal.second-=1;

    for(int i=0;i<H;++i){
        for(int j=0;j<W;++j){
            char buf;
            std::cin>>buf;
            grid& p=pond[i][j];
            if (buf=='.'){
                p.isBarier=0;
                if(start.first==i && start.second==j){
                    for(int direct=0;direct<NUM_DIRECT;++direct){
                        q.push(priorityNode(indexWithDirect(index(i,j),(char)direct),0));
                        p.cost[direct]=0;
                    }
                }
            }else{
                p.isBarier=1;
                p.isSearched=1;
            }
        }
    }

    while(!q.empty()){
        priorityNode    node=q.top();
        indexWithDirect iwd=node.first;
        index           id=iwd.first;
        char            direct=iwd.second;
        double          cost=node.second;
        auto&           minCost=pond[id.first][id.second].cost[direct];
        double          goalCost=pond[goal.first][goal.second].cost[direct];
        q.pop();
        // print(node);
        if(minCost<cost){
            continue;
        }
        if(goalCost<cost){
            continue;
        }
        minCost=cost;

        for(int j=1;j<=K;++j){
            auto calcIndex=moveV(id,direct,j);
            if(isEnableMove(calcIndex,pond)==1){
                auto& calcGrid=pond[calcIndex.first][calcIndex.second];
                auto& calcGridCost=calcGrid.cost;
                for(char k=0;k<NUM_DIRECT;++k){
                    double movedCost=calcCost(cost,direct,k,j,K);
                    if(calcGridCost[k]>movedCost){
                        calcGridCost[k]=movedCost;
                        q.push(priorityNode(indexWithDirect(calcIndex,k),movedCost));
                        calcGrid.isSearched=1;
                    }
                }
            }
            else{
                break;
            }
        }
    }
    grid goalGrid=pond[goal.first][goal.second];
    if(goalGrid.isSearched==0){
        std::cout<<-1<<std::endl;
    }else{
        double ans=goalGrid.cost[0];
        for(int i=0;i<NUM_DIRECT;++i){
            if(ans>goalGrid.cost[i]){
                ans=goalGrid.cost[i];
            }
        }
        std::cout<<(int)(ceil(ans))<<std::endl;
    }
    // print(pond);
}

int main(){
    dijkstra();
    return 0;
}