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: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define rep(i,k,n) for(int (i)=(k);(i)<(n);(i)++)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) ((a)>0 ?(a):-(a))
#define llong long long

struct heap_data{
	int num;		// データ番号 data[x].num = x
	int dis;		// スタートからの距離
	int block;		// 通行可能なら0,不可なら1
	int flag;		// 初期値1, 調べた後に0にする
	int heap_num;	// 二分木内の位置 heap[data->heap_num]=data
// 問題に応じて枝データを入力せよ
};

// データ,二分木,スタート番号
void start_set(struct heap_data*,struct heap_data**,int);

// データの比較( a<b -> return>0 )
int cmp_data(struct heap_data*,struct heap_data*);

// disが小さくなったときに適切な位置まで上げる
// ヒープデータ,disが小さくなったデータ番号
void heap_up(struct heap_data**,int);
// swapで上まで持ってきた時に適切な位置まで下げる
// ヒープデータ,上まで持ってきたデータ番号,全体のデータ数
void heap_down(struct heap_data**,int,int);
// 指定されたヒープデータを入れ替える
// ヒープデータ,入れ替えたいデータ番号二つ
void swap_heap(struct heap_data**,int,int);

void dijkstra(struct heap_data*,struct heap_data**,int);

int H,W,K;

int main(){
	scanf("%d %d %d",&H,&W,&K);
	int x1,y1,x2,y2;
	scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
	x1--;y1--;x2--;y2--;
	int N=H*W;	// データ数
	struct heap_data data[N];	// データ
	struct heap_data *heap[N];	// 上のデータを要素に持つ二分木
	rep(i,0,N) heap[i] = data+i;// 初期heapを構成
	rep(i,0,N){
		(data+i)->num = i;
		(data+i)->dis = 2000000000;
		char c; scanf("%c",&c);
		if( c=='.' ) (data+i)->block = 0;
		else if( c=='@' ) (data+i)->block = 1;
		else{
			scanf("%c",&c);
			if( c=='.' ) (data+i)->block = 0;
			else if( c=='@' ) (data+i)->block = 1;
		}
		(data+i)->flag = 1;
		(data+i)->heap_num = i;
	}
	start_set(data,heap,x1*W+y1);
	dijkstra(data,heap,N);
	if(data[x2*W+y2].dis==2000000000) printf("-1");
	else printf("%d",data[x2*W+y2].dis);
	return 0;
}

void start_set(struct heap_data* data,struct heap_data** heap,int k){
	(data+k)->dis = 0;	// 距離0
	swap_heap(heap,0,k);// 二分木の一番上にデータを移す
}

int cmp_data(struct heap_data* data1,struct heap_data* data2){
	if( data1->flag==0 ) return -1;
	if( data1->block==1) return -1;
	if( data2->flag==0 ) return 1;
	if( data1->block==1) return 1;
	return data2->dis - data1->dis;
}

void heap_up(struct heap_data** heap,int n){
	if( n==0 ) return;
	int k = (n-1)/2;	// 二分木における一個上のデータ
	if( cmp_data(*(heap+n),*(heap+k)) > 0 ){
		swap_heap(heap,n,k);
		return heap_up(heap,k);
	}
	return;
}

void heap_down(struct heap_data **heap,int k,int N){
	int left=k*2+1,right=k*2+2;
	if( (right<N) && cmp_data(*(heap+right),*(heap+left))>0 ){
		if( cmp_data(*(heap+right),*(heap+k))>0 ){
			// *(heap+right)が最小
			swap_heap(heap,right,N);
			return heap_down(heap,right,N);
		}
	}else if( (left<N) && cmp_data(*(heap+left),*(heap+k))>0 ){
		swap_heap(heap,left,N);
		return heap_down(heap,left,N);
	}
	return;
}

void swap_heap(struct heap_data **heap,int i,int j){
	struct heap_data *temp;
	temp = *(heap+i);
	*(heap+i) = *(heap+j);
	*(heap+j) = temp;
	(*(heap+i))->heap_num = i;
	(*(heap+j))->heap_num = j;
}

void dijkstra(struct heap_data *data,struct heap_data **heap,int N){
	int i=0;	// 調べたデータ数
	struct heap_data *data1,*data2;
	while( ((data1=(*heap))->flag!=0) && i<N ){
		//printf("%d %d %d %d\n",data1->num/W,data1->num%W,data1->block,data1->dis);
		data1->flag = 0;
		swap_heap(heap,0,N-i-1);	// data1を一番下に移す
		heap_down(heap,0,N-i-1);	// 持ち上げられたデータを下げる
		// 距離演算,書き換えたらheap_upすること
		int j=data1->num-W,cnt=0;
		while(j>=0&&cnt<K){	//上方向
			if( data[j].block==1 ) break;
			else if( data1->dis < data[j].dis ){
				data[j].dis = data1->dis + 1;
				heap_up(heap,data[j].heap_num);
			}
			j-=W; cnt++;
		}
		j = data1->num-1; cnt=0;
		while((j+1)%W!=0&&cnt<K){	//左方向
			if( data[j].block==1 ) break;
			else if( data1->dis < data[j].dis ){
				data[j].dis = data1->dis + 1;
				heap_up(heap,data[j].heap_num);
			}
			j--; cnt++;
		}
		j = data1->num+1; cnt=0;
		while(j%W!=0&&cnt<K){	//右方向
			if( data[j].block==1 ) break;
			else if( data1->dis < data[j].dis ){
				data[j].dis = data1->dis + 1;
				heap_up(heap,data[j].heap_num);
			}
			j++; cnt++;
		}
		j = data1->num+W; cnt=0;
		while(j<N&&cnt<K){
			if( data[j].block==1 ) break;
			else if( data1->dis < data[j].dis ){
				data[j].dis = data1->dis + 1;
				heap_up(heap,data[j].heap_num);
			}
			j+=W; cnt++;
		}
		i++;
	}
	return;
}