# Codeforces 877 D. Olya and Energy drinks (BFS) __ Search

Source: Internet
Author: User

Description

Olya loves energy drinks. She loves them so much as that her room are full of empty cans to energy drinks.

Formally, her room can be represented as a field of nxm cells, each cell of the which is 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 is needs to the get from cell (x1, y1) to cell (x2, y2). How many seconds'll it take her if she moves optimally?

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

Input

The contains three integers n, m and K (1≤n, M, k≤1000)-the sizes of the room and Olya.

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 the ' the ' 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.

examples Input

```3 4 4
...
. ###.
....
1 1 3 1
```

examples Output

```3
```

the

From (x1,y1) (x_1,y_1) to (X2,y2) (x_2,y_2), the maximum in one Direction to go K-step, and not through the wall, asked at least several times to achieve the goal.

train of Thought

Each time you can go to the point to join the queue BFS, pay attention to optimize pruning, as a point only calculate once.

AC Code

```#include <bits/stdc++.h> #define IO Ios::sync_with_stdio (false); \ cin.tie (0); \ cout.tie (0);
#define INF 0x7f7f7f7f using namespace std;
typedef __int64 LL;
const int MAXM = 1E3+10;
const int MAXN = 1E7+10;

const int mv[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
struct node {int x;
int y;
int t;
Node () {} node (int xx,int yy,int tt) {x = xx;
y = yy;
t = TT;

}} G[MAXN];
Char C[MAXM][MAXM];
int F[MAXM][MAXM];
int n,m,k;
int x1,x2,y1,y2;

queue<node>que; void Init () {for (int j=0; j<4; j) for (int i=1; i<=k; i++) {int xx = x1+mv[j][0]*
I
int yy = Y1+mv[j][1]*i;
if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&c[xx][yy]== '. ')
{f[xx][yy]=1;
Que.push (Node (xx,yy,1));
else break;
} void BFs () {if (x1==x2&&y1==y2) {puts ("0"); REturn;
Init ();
while (!que.empty ()) {node S=que.front ();
Que.pop ();
if (F[x2][y2]) {printf ("%d\n", F[x2][y2]);
Return for (int j=0; j<4; j + +) for (int i=1; i<=k; i++) {int xx = S.x+mv[j
][0]*i;
int yy = S.y+mv[j][1]*i;
if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&c[xx][yy]== '. ')
{if (f[xx][yy]==0) {f[xx][yy]=s.t+1;
Que.push (Node (xx,yy,s.t+1));
} else break;
} puts ("-1");
int main () {scanf ("%d%d%d%*c", &n,&m,&k);
for (int i=1; i<=n; i++) gets (c[i]+1);
scanf ("%d%d%d%d", &x1,&y1,&x2,&y2);
BFS ();
return 0; }```
Related Keywords:
Related Article

### Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: