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# (Mono-mcs 6.8.0.105) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
  class Program
  {
    static void Main(string[] args)
    {
      ABC170.F();
    }
    class ABC170
    {
      public static void A()
      {
        var X = Read.IntN().ToList();
        Console.WriteLine(X.IndexOf(0) + 1);
      }
      public static void B()
      {
        var X = Read.Int();
        var Y = Read.Int();
        Console.WriteLine(X * 2 <= Y && Y <= 4 * X && Y % 2 == 0 ? "Yes" : "No");
      }
      public static void C()
      {
        var X = Read.Int();
        var N = Read.Int();
        var P = new int[0];
        if (N > 0)
        {
          P = Read.IntN();
        }
        var Y = 99999;
        for(int i = -100; i <= 200; ++i)
        {
          if(!P.Contains(i) && Math.Abs(X - Y) > Math.Abs(X - i))
          {
            Y = i;
          }
        }
        Console.WriteLine(Y);
      }
      public static void D()
      {
        var N = Read.Int();
        var A = Read.IntN();
        var freq = Util.Frequency(A);
        var below1000 = new HashSet<int>();
        foreach(var kvp in freq)
        {
          if (kvp.Key <= 1000)
          {
            below1000.Add(kvp.Key);
          }
        }
        int count = 0;
        foreach(var a in A)
        {
          bool ok = true;
          foreach(var b in below1000)
          {
            if (a % b == 0)
            {
              if (a != b || freq[a] > 1)
              {
                ok = false;
                break;
              }
            }
          }
          if (!ok) { continue; }
          for(int i = 1; i <= 1000; ++i)
          {
            if (a % i == 0)
            {
              var d = a / i;
              if (freq.ContainsKey(d) && (i > 1 || freq[d] > 1))
              {
                ok = false;
                break;
              }
            }
          }
          if (ok) { count++; }
        }
        Console.WriteLine(count);
      }
      public static void E()
      {
        var N = Read.Int();
        var Q = Read.Int();
        var AB = Read.TupleInt(N);
        var CD = Read.TupleInt(Q);
        var count = new int[200001];
        var belong = new int[200001];
        var sets = new Util.MultiSet<int>[200001];
        for(int i = 0; i < sets.Length; ++i)
        {
          sets[i] = new Util.MultiSet<int>();
        }
        for(int i=0;i<N;++i)
        {
          var A = AB[i].Item1;
          var B = AB[i].Item2;
          count[B]++;
          belong[i] = B;
          sets[B].Insert(A);
        }
        var highest = new Util.MultiSet<int>();
        for(int i = 1; i < count.Length; ++i)
        {
          if (count[i] > 0)
          {
            highest.Insert(sets[i].ElementAt(count[i] - 1));
          }
        }
        var ans = new List<int>();
        foreach(var cd in CD)
        {
          var C = cd.Item1;
          var D = cd.Item2;
          var a = AB[C - 1].Item1;
          var b = belong[C - 1];
          var hc1 = sets[b].ElementAt(count[b] - 1);
          count[b]--;
          sets[b].Remove(a);
          if (count[b] == 0)
          {
            highest.Remove(a);
          }
          else
          {
            if (a == hc1)
            {
              var newh = sets[b].ElementAt(count[b] - 1);
              if (hc1 != newh)
              {
                highest.Remove(hc1);
                highest.Insert(newh);
              }
            }
          }
          var hc2 = count[D] == 0 ? 0 : sets[D].ElementAt(count[D] - 1);
          count[D]++;
          belong[C - 1] = D;
          sets[D].Insert(a);
          if (count[D] == 1)
          {
            highest.Insert(a);
          }
          else
          {
            var newh = sets[D].ElementAt(count[D] - 1);
            if (hc2 != newh)
            {
              highest.Remove(hc2);
              highest.Insert(newh);
            }
          }
          ans.Add(highest.ElementAt(0));
        }
        Console.WriteLine(String.Join(Environment.NewLine, ans));
      }
      public static void F()
      {
        var H = Read.Int();
        var W = Read.Int();
        var K = Read.Int();
        var X1 = Read.Int();
        var Y1 = Read.Int();
        var X2 = Read.Int();
        var Y2 = Read.Int();
        var C = Enumerable.Range(0, H).Select(i => Read.Str()).ToArray();
        var board = new bool[H + 2, W + 2];
        for(int i = 0; i < H; ++i)
        {
          for (int j = 0; j < W; ++j)
          {
            if (C[i][j] == '.')
            {
              board[i + 1, j + 1] = true;
            }
          }
        }
        var dx = new int[] { 1, -1, 0, 0 };
        var dy = new int[] { 0, 0, 1, -1 };
        var dist = Util.Gen(H + 2, W + 2, -1);
        var nextDist = Enumerable.Range(0, 4).Select(i => Util.Gen(H + 2, W + 2, -1)).ToArray();
        dist[X1, Y1] = 0;
        var queue = new Queue<Tuple<int, int>>();
        queue.Enqueue(new Tuple<int, int>(X1, Y1));
        while (queue.Count > 0)
        {
          var t = queue.Dequeue();
          for(int i = 0; i < 4; ++i)
          {
            for (int j = 1; j <= K; ++j)
            {
              var X = t.Item1 + dx[i] * j;
              var Y = t.Item2 + dy[i] * j;
              if(board[X,Y] && dist[X, Y] < 0)
              {
                dist[X, Y] = dist[t.Item1, t.Item2] + 1;
                nextDist[i][X,Y] = dist[t.Item1, t.Item2] + 1;
                queue.Enqueue(new Tuple<int, int>(X, Y));
              }
              else
              {
                if (!board[X, Y] || dist[X, Y] <= dist[t.Item1, t.Item2] || nextDist[i][X, Y] == dist[t.Item1, t.Item2] + 1)
                {
                  break;
                }
              }
            }
          }
        }
        Console.WriteLine(dist[X2, Y2]);
      }
    }

    class Util
    {
      public static T[,] Gen<T>(int r, int c, T v)
      {
        var matrix = new T[r, c];
        for (int i = 0; i < r; ++i)
        {
          for (int j = 0; j < c; ++j)
          {
            matrix[i, j] = v;
          }
        }
        return matrix;
      }
      public static long[] ASum(long[] array)
      {
        var res = new long[array.Length + 1];
        for (int i = 0; i < array.Length; ++i)
        {
          res[i + 1] = res[i] + array[i];
        }
        return res;
      }
      public static long[,] ASum(long[,] matrix)
      {
        var N = matrix.GetLength(0);
        var M = matrix.GetLength(1);
        var res = new long[N + 1, M + 1];
        for(int i = 0; i < N; ++i)
        {
          for(int j = 0; j < M; ++j)
          {
            res[i + 1, j + 1] = res[i, j + 1] + res[i + 1, j] - res[i, j] + matrix[i, j];
          }
        }
        return res;
      }
      public static Dictionary<T, long> Frequency<T>(T[] array)
      {
        var dict = new Dictionary<T, long>();
        foreach (var a in array)
        {
          if (!dict.ContainsKey(a)) { dict.Add(a, 0); }
          dict[a]++;
        }
        return dict;
      }
      public static HashSet<long> Divisor(long x)
      {
        var div = new HashSet<long>(); for (long p = 1; p * p <= x; ++p) { if (x % p == 0) { div.Add(p); } }
        foreach (var d in div.ToArray())
        {
          if (!div.Contains(x / d)) { div.Add(x / d); }
        }
        return div;
      }
      public class BST<T> where T : IComparable
      {
        public class Node
        {
          public T V;
          public Node L, R;
          public int Count;
          public Node(T v) { V = v; Count = 1; }
        }
        static Random _rnd = new Random();
        public static int Count(Node t) { return t == null ? 0 : t.Count; }
        static Node Update(Node t) { t.Count = Count(t.L) + Count(t.R) + 1; return t; }
        public static Node Merge(Node l, Node r)
        {
          if (l == null || r == null) return l == null ? r : l;
          if ((double)Count(l) / (double)(Count(l) + Count(r)) > _rnd.NextDouble())
          { l.R = Merge(l.R, r); return Update(l); }
          else { r.L = Merge(l, r.L); return Update(r); }
        }
        public static Tuple<Node, Node> Split(Node t, int k)
        {
          if (t == null) return new Tuple<Node, Node>(null, null);
          if (k <= Count(t.L))
          {
            var s = Split(t.L, k);
            t.L = s.Item2;
            return new Tuple<Node, Node>(s.Item1, Update(t));
          }
          else
          {
            var s = Split(t.R, k - Count(t.L) - 1);
            t.R = s.Item1;
            return new Tuple<Node, Node>(Update(t), s.Item2);
          }
        }
        public static Node Remove(Node t, T v)
        {
          if (Find(t, v) == null) return t;
          return RemoveAt(t, LowerBound(t, v));
        }
        public static Node RemoveAt(Node t, int k)
        {
          var s = Split(t, k);
          var s2 = Split(s.Item2, 1);
          return Merge(s.Item1, s2.Item2);
        }
        public static bool Contains(Node t, T v) { return Find(t, v) != null; }
        public static Node Find(Node t, T v)
        {
          while (t != null)
          {
            var cmp = t.V.CompareTo(v);
            if (cmp > 0) t = t.L;
            else if (cmp < 0) t = t.R;
            else break;
          }
          return t;
        }

        public static Node FindByIndex(Node t, int idx)
        {
          if (t == null) return null;
          var currentIdx = Count(t) - Count(t.R) - 1;
          while (t != null)
          {
            if (currentIdx == idx) return t;
            if (currentIdx > idx)
            { t = t.L; currentIdx -= (Count(t == null ? null : t.R) + 1); }
            else { t = t.R; currentIdx += (Count(t == null ? null : t.L) + 1); }
          }
          return null;
        }
        public static int UpperBound(Node t, T v)
        {
          var torg = t;
          if (t == null) return -1;

          var ret = Int32.MaxValue;
          var idx = Count(t) - Count(t.R) - 1;
          while (t != null)
          {
            var cmp = t.V.CompareTo(v);

            if (cmp > 0)
            {
              ret = Math.Min(ret, idx);
              t = t.L;
              idx -= (Count(t == null ? null : t.R) + 1);
            }
            else if (cmp <= 0)
            {
              t = t.R;
              idx += (Count(t == null ? null : t.L) + 1);
            }
          }
          return ret == Int32.MaxValue ? Count(torg) : ret;
        }
        public static int LowerBound(Node t, T v)
        {
          var torg = t;
          if (t == null) return -1;

          var idx = Count(t) - Count(t.R) - 1;
          var ret = Int32.MaxValue;
          while (t != null)
          {
            var cmp = t.V.CompareTo(v);
            if (cmp >= 0)
            {
              if (cmp == 0) ret = Math.Min(ret, idx);
              t = t.L;
              if (t == null) ret = Math.Min(ret, idx);
              idx -= t == null ? 0 : (Count(t.R) + 1);
            }
            else if (cmp < 0)
            {
              t = t.R;
              idx += (Count(t == null ? null : t.L) + 1);
              if (t == null) return idx;
            }
          }
          return ret == Int32.MaxValue ? Count(torg) : ret;
        }
        public static Node Insert(Node t, T v)
        {
          var ub = LowerBound(t, v);
          return InsertByIdx(t, ub, v);
        }
        static Node InsertByIdx(Node t, int k, T v)
        {
          var s = Split(t, k);
          return Merge(Merge(s.Item1, new Node(v)), s.Item2);
        }
        public static IEnumerable<T> Enumerate(Node t)
        {
          var ret = new List<T>();
          Enumerate(t, ret);
          return ret;
        }
        static void Enumerate(Node t, List<T> ret)
        {
          if (t == null) return;
          Enumerate(t.L, ret);
          ret.Add(t.V);
          Enumerate(t.R, ret);
        }
      }
      public class Set<T> where T : IComparable
      {
        protected BST<T>.Node _root;
        public T this[int idx] { get { return ElementAt(idx); } }
        public int Count() { return BST<T>.Count(_root); }
        public virtual void Insert(T v)
        {
          if (_root == null) _root = new BST<T>.Node(v);
          else
          {
            if (BST<T>.Find(_root, v) != null) return;
            _root = BST<T>.Insert(_root, v);
          }
        }
        public void Clear() { _root = null; }
        public void Remove(T v) { _root = BST<T>.Remove(_root, v); }
        public bool Contains(T v) { return BST<T>.Contains(_root, v); }
        public T ElementAt(int k)
        {
          var node = BST<T>.FindByIndex(_root, k);
          if (node == null) throw new IndexOutOfRangeException();
          return node.V;
        }
        public int Count(T v) { return BST<T>.UpperBound(_root, v) - BST<T>.LowerBound(_root, v); }
        public int LowerBound(T v) { return BST<T>.LowerBound(_root, v); }
        public int UpperBound(T v) { return BST<T>.UpperBound(_root, v); }
        public Tuple<int, int> EqualRange(T v)
        {
          if (!Contains(v)) return new Tuple<int, int>(-1, -1);
          return new Tuple<int, int>(BST<T>.LowerBound(_root, v), BST<T>.UpperBound(_root, v) - 1);
        }
        public List<T> ToList() { return new List<T>(BST<T>.Enumerate(_root)); }
      }
      public class MultiSet<T> : Set<T> where T : IComparable { public override void Insert(T v) { if (_root == null) { _root = new BST<T>.Node(v); } else { _root = BST<T>.Insert(_root, v); } } }
    }
    class Bound
    {
      public static int Lowerbound<T1, T2>(SortedList<T1, T2> list, T1 x) where T1 : IComparable<T1>
      {
        return Lowerbound(list.Keys, x);
      }
      public static int Upperbound<T1, T2>(SortedList<T1, T2> list, T1 x) where T1 : IComparable<T1>
      {
        return Upperbound(list.Keys, x);
      }
      public static int Lowerbound<T>(IList<T> list, T x) where T : IComparable<T>
      {
        int min = 0;
        int max = list.Count;
        while (min < max)
        {
          var mid = (min + max) / 2;
          if (list[mid].CompareTo(x) >= 0)
          {
            max = mid;
          }
          else
          {
            min = mid + 1;
          }
        }
        return max;
      }
      public static int Upperbound<T>(IList<T> list, T x) where T : IComparable<T>
      {
        int min = 0;
        int max = list.Count;
        while (min < max)
        {
          var mid = (min + max) / 2;
          if (list[mid].CompareTo(x) > 0)
          {
            max = mid;
          }
          else
          {
            min = mid + 1;
          }
        }
        return max;
      }
      public static int UpperboundInt(List<int> list, int x)
      {
        int min = 0;
        int max = list.Count;
        while (min < max)
        {
          var mid = (min + max) / 2;
          if (list[mid] > x)
          {
            max = mid;
          }
          else
          {
            min = mid + 1;
          }
        }
        return max;
      }
    }

    static class Read
{
static Queue<string> queue = new Queue<string>();
static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
public static ulong[] ULongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToUInt64(s)).ToArray(); }
public static string[] StrN() { return Console.ReadLine().Split(' '); }
public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
public static long[,] LongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = Read.LongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; }
public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M, bool isDirectional = false)
{
Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); }
        for (int i = 0; i < M; ++i) { var ab = Read.IntN(); if (!dict[ab[0]].Contains(ab[1])) { dict[ab[0]].Add(ab[1]); } if (!isDirectional && !dict[ab[1]].Contains(ab[0])) { dict[ab[1]].Add(ab[0]); } }
        return dict;
}
public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
{
Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new Dictionary<int, long>()); }
for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1], ab[2]); dict[ab[1]].Add(ab[0], ab[2]); }
return dict;
}
}
    }
}