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# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using CompLib.Util;

public class Program
{
    private int H, W, K;
    private int X1, Y1, X2, Y2;
    private char[][] C;

    private int[] Dx = new int[] {0, 1, 0, -1};

    private int[] Dy = new int[] {1, 0, -1, 0};

    public void Solve()
    {
        var sc = new Scanner();
        H = sc.NextInt();
        W = sc.NextInt();
        K = sc.NextInt();
        X1 = sc.NextInt() - 1;
        Y1 = sc.NextInt() - 1;
        X2 = sc.NextInt() - 1;
        Y2 = sc.NextInt() - 1;
        C = new char[H][];
        for (int i = 0; i < H; i++)
        {
            C[i] = sc.NextCharArray();
        }

        if (K == 1)
        {
            var xqq = new Queue<int>();
            var yqq = new Queue<int>();
            int[,] tt = new int[H, W];
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    tt[i, j] = int.MaxValue;
                }
            }

            xqq.Enqueue(X1);
            yqq.Enqueue(Y1);

            while (xqq.Count > 0)
            {
                var xx = xqq.Dequeue();
                var yy = yqq.Dequeue();
                for (int i = 0; i < 4; i++)
                {
                    int nx = xx + Dx[i];
                    int ny = yy + Dy[i];
                    if (nx < 0 || nx >= H) continue;
                    if (ny < 0 || ny >= W) continue;
                    if (C[nx][ny] == '@') continue;

                    if (tt[nx, ny] < tt[xx, yy] + 1)
                    {
                        xqq.Enqueue(nx);
                        yqq.Enqueue(ny);
                        tt[nx, ny] = tt[xx, yy] + 1;
                    }
                }
            }

            if (tt[X2, Y2] == int.MaxValue) Console.WriteLine("-1");
            else Console.WriteLine(tt[X2, Y2]);
            return;
        }

        // 距離、何ターン目か、向き?
        (int d, int r)[] t = new (int d, int r)[H * W * 4];
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    t[Co(i, j, k)] = (int.MaxValue, K);
                }
            }
        }

        var xq = new Queue<int>();
        var yq = new Queue<int>();
        var dq = new Queue<int>();
        for (int i = 0; i < 4; i++)
        {
            xq.Enqueue(X1);
            yq.Enqueue(Y1);
            dq.Enqueue(i);
            t[Co(X1, Y1, i)] = (0, 0);
        }

        while (xq.Count > 0)
        {
            int xx = xq.Dequeue();
            int yy = yq.Dequeue();
            int dd = dq.Dequeue();
            for (int i = 0; i < 4; i++)
            {
                int nx = xx + Dx[i];
                int ny = yy + Dy[i];
                if (nx < 0 || nx >= H) continue;
                if (ny < 0 || ny >= W) continue;
                if (C[nx][ny] == '@') continue;
                int nr;
                int nd;
                if (i == dd)
                {
                    nr = (t[Co(xx, yy, dd)].r + 1) % K;
                }
                else
                {
                    nr = 1;
                }

                if (nr == 1)
                {
                    nd = t[Co(xx, yy, dd)].d + 1;
                }
                else
                {
                    nd = t[Co(xx, yy, dd)].d;
                }

                if (nd < t[Co(nx, ny, i)].d || (nd == t[Co(nx, ny, i)].d && nr < t[Co(nx, ny, i)].r))
                {
                    xq.Enqueue(nx);
                    yq.Enqueue(ny);
                    dq.Enqueue(i);
                    t[Co(nx, ny, i)] = (nd, nr);
                }
            }
        }

        int ans = int.MaxValue;
        for (int i = 0; i < 4; i++)
        {
            ans = Math.Min(ans, t[Co(X2, Y2, i)].d);
        }

        if (ans == int.MaxValue)
        {
            Console.WriteLine("-1");
        }
        else
        {
            Console.WriteLine(ans);
        }
    }

    int Co(int i, int j, int k)
    {
        return i * W * 4 + j * 4 + k;
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}