Running

Interview

Time limit1sMemory limit512 MB

Summary
On a grid with walls, each move slides 1 to K empty cells in one of four directions; find the minimum number of moves from start to goal.
Level

Medium7 of 10

Topics
BFS, Graph, Array, Shortest path
Solved
No attempts yet

Problem

Jinyoung wants to run in a gym of size N×MN \times M to lose weight. The gym is divided into 1×11 \times 1 cells, and each cell is either empty or a wall. The cell in row xx, column yy is denoted (x,y)(x, y).

Every second, Jinyoung chooses one direction among up, down, right, and left, and moves at least 1 and at most KK empty cells in that direction.

Given the start point (x1,y1)(x_1, y_1) and the end point (x2,y2)(x_2, y_2), find the minimum time to move from the start point to the end point.

Input

The first line gives the size of the gym NN and MM, and the maximum number of cells KK that can be moved in 1 second.

From the second line, NN lines give the state of the gym. Each cell of the gym is either empty or a wall, where an empty cell is given as '.', and a wall is given as '#'.

The last line gives four integers x1x_1, y1y_1, x2x_2, y2y_2. The two cells are different cells and are always empty.

Output

Print the minimum time to move from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2). If it is impossible to move, print -1.

Constraints

  • 2≤N,M≤1 0002 \le N, M \le 1\,000
  • 1≤K≤1 0001 \le K \le 1\,000
  • 1≤x1,x2≤N1 \le x_1, x_2 \le N
  • 1≤y1,y2≤M1 \le y_1, y_2 \le M

Examples3

  1. Example 1

    Input
    3 4 4
    ....
    ###.
    ....
    1 1 3 1
    
    Expected output
    3
    
  2. Example 2

    Input
    3 4 1
    ....
    ###.
    ....
    1 1 3 1
    
    Expected output
    8
    
  3. Example 3

    Input
    2 2 1
    .#
    #.
    1 1 2 2
    
    Expected output
    -1