Home > Industries > Energy

Source: Internet
Author: User

Olya loves energy drinks. She loves them so much, She is the full of the empty cans from the energy drinks.

Formally, her hostel can be represented as a field of nxm cells, each cell of the which is the empty or littered with cans.

Olya drank a lot of energy drink, so now she can run K meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to K meters in this direction . Of course, she can only run through empty cells.

Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds would it take her if she moves optimally?

It's guaranteed that cells (x1, y1) and (x2, y2) is empty. These cells can coincide.

Input

The first line contains three integers n, m and K (1≤n, M, k≤1000)-the sizes of the "the" and Olya ' s speed.

Then n lines follow containing M characters each, the i-th of them contains on j-th position ' # ', if the cell (i, j) is Li Ttered with cans, and "." otherwise.

The last line contains four integers x1, y1, x2, y2 (1≤x1, X2≤n, 1≤y1, y2≤m)-the coordinates of the first and th e last cells.

Output

Print a single integer-the minimum time it'll take Olya to get from (x1, y1) to (x2, y2).

If it ' s impossible to get from (x1, y1) to (x2, y2), print-1. The **following:**

Feel Noip pills ... This problem is not done ... Since each point will only be accessed once, then we just need to quickly find a point up and down four directions not visited the point on the line, which can be used and check the set implementation, the rest is the conventional BFS. **Code:**

#include <bits/stdc++.h> using namespace std; #define PA pair<int,int> #define LL Long Long const int maxn=1010; int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x= (x<<3) + (x<<1) + (ch^48); Ch=getchar ();} return x*f; } int k,n,m,stx,sty,edx,edy; int mp[maxn][maxn],num[maxn][maxn],nnum[maxn*maxn][2],tot=0; queue<int>qx,qy; BOOL VIS[MAXN*MAXN]; int F[MAXN*MAXN]; int fa[4][maxn*maxn];//0123 up or down int findfa (int o,int x) {return (fa[o][x]==x) X:FA[O][X]=FINDFA (o,fa[o][x])); void del (int o,int x,int y) {Fa[0][o]=findfa (0,fa[0][num[x-1][y]); FA[1][O]=FINDFA (1,fa[1][num[x+1][y]); FA[2][O]=FINDFA (2,fa[2][num[x][y-1]); FA[3][O]=FINDFA (3,fa[3][num[x][y+1]); } int main () {memset (f,63,sizeof (f)); N=read (); M=read (); K=read (); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) num[i][j]=++tot,nnum[tot][0]=i,nnum[tot][1]=j; For(int i=1;i<=n;i++) {char STR[MAXN]; scanf ("%s", str+1); for (int j=1;j<=m;j++) {mp[i][j]= ((str[j]== ' # ')? 0:1); for (int l=0;l<4;l++) FA[L][NUM[I][J]]=NUM[I][J]; }} stx=read (), Sty=read (), Edx=read (), Edy=read (); Qx.push (STX); Qy.push (sty); int t=num[stx][sty];f[t]=0;vis[t]=true; Fa[0][t]=num[stx-1][sty];fa[1][t]=num[stx+1][sty]; FA[2][T]=NUM[STX][STY-1];FA[3][T]=NUM[STX][STY+1]; while (!qx.empty ()) {int X=qx.front (), Y=qy.front (); if (x==edx&&y==edy) break; Qx.pop (); Qy.pop (); int t,o=num[x][y]; for (int i=0;i<4;i++) {while ((T=FINDFA (I,fa[i][o)))!=0) {int tx=nnum[t ][0],TY=NNUM[T][1]; if (!mp[tx][ty]) break; if (i==0&&x-tx>k) break; else if (i==1&&tx-x>k) break; else if (i==2&&y-ty>k) break; else if (i==3&&ty-y>k) break; Vis[t]=true; f[t]=f[o]+1; Del (t,tx,ty); Qx.push (TX); Qy.push (Ty); }}} int ans=f[num[edx][edy]]; if (ans!=1061109567) printf ("%d", ans); Else puts ("-1"); }