D. Olya and Energy Drinks
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.

Formally, her room can be represented as a field of n × m cells, each cell of 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 needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will 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 first line contains three integers nm and k (1 ≤ n, m, k ≤ 1000) — the sizes of the room 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 littered 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 the last cells.

Output

Print a single integer — the minimum time it will 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
output
3
input
3 4 1
....
###.
....
1 1 3 1
output
8
input
2 2 1
.#
#.
1 1 2 2
output
-1
Note

In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.

In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.

Olya does not recommend drinking energy drinks and generally believes that this is bad.

//题意:给出一个 n*m 的图,每一秒可以走 1 -- k 步,给出起点,终点,问最少需要几秒?

//用bfs是肯定的,bfs能到达的所有的地方,但是,如何不重复搜是个问题,用 vis 数组标记该点从向哪个方向搜过了,不重复搜即可。用位运算标记比较好,这样最多搜 n*m*4 次吧

 # include <bits/stdc++.h>
using namespace std;
# define eps 1e-
# define INF 1e20
# define pi acos(-1.0)
# define MX
const int dir[][]={{-,},{,},{,},{,-}};
struct Node
{
int x, y;
int t;
}; int n, m, k;
char G[MX][MX];
int sx, sy, ex, ey;
int ans[MX][MX];
int vis[MX][MX]; int check(Node &x)
{
if (x.x<||x.x>n||x.y<||x.y>m) return ;
if (G[x.x][x.y]=='#') return ;
return ;
} void bfs()
{
memset(vis,,sizeof(vis));
memset(ans,-,sizeof(ans));
queue<Node> q;
q.push((Node){sx,sy,});
vis[sx][sy]=(<<)-;
ans[sx][sy]=;
while (!q.empty())
{
Node nex, now = q.front();
q.pop();
nex.t = now.t+;
for (int i=;i<;i++)
{
for (int j=;j<=k;j++)
{
nex.x = now.x+dir[i][]*j;
nex.y = now.y+dir[i][]*j;
if (!check(nex)) break;
if (vis[nex.x][nex.y] & (<<i)) break;
if (!vis[nex.x][nex.y])
{
q.push(nex);
ans[nex.x][nex.y]=nex.t;
}
vis[nex.x][nex.y]|=(<<i);
}
}
}
} int main()
{
scanf("%d%d%d",&n,&m,&k);
for (int i=;i<=n;i++)
scanf("%s",G[i]+);
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
bfs();
printf("%d\n",ans[ex][ey]);
return ;
}

Olya and Energy Drinks(bfs)的更多相关文章

  1. cf 442 D. Olya and Energy Drinks

    cf 442 D. Olya and Energy Drinks(bfs) 题意: 给一张\(n \times m(n <= 1000,m <= 1000)\)的地图 给出一个起点和终点, ...

  2. Codeforces 877 D. Olya and Energy Drinks

    http://codeforces.com/contest/877/problem/D   D. Olya and Energy Drinks time limit per test 2 second ...

  3. Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks

    题目链接:http://codeforces.com/contest/877/problem/D D. Olya and Energy Drinks time limit per test2 seco ...

  4. 【Codeforces Round #442 (Div. 2) D】Olya and Energy Drinks

    [链接] 我是链接,点我呀:) [题意] 给一张二维点格图,其中有一些点可以走,一些不可以走,你每次可以走1..k步,问你起点到终点的最短路. [题解] 不能之前访问过那个点就不访问了.->即k ...

  5. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. Codeforces Round #442 Div.2 A B C D E

    A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...

  7. [转]Speeding Up Websites With YSlow

    本文转自:http://net.tutsplus.com/tutorials/other/speeding-up-websites-with-yslow/ We all know there are ...

  8. 洛谷P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler

    P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler 题目描述 Farmer John has purchased the world's most l ...

  9. 使用现代C++如何避免bugs(下)

     使用现代C++如何避免bugs(下) About virtual functions Virtual functions hinder a potential problem: the thing ...

随机推荐

  1. CentOS 6.4 图文安装教程(有些设置大部分教程没出现过)

    http://www.jb51.net/os/78318.html CentOS 6.4 下载地址: http://www.jb51.net/softs/78243.html 1.首先,要有一张Cen ...

  2. vue 表单验证实例

    1.注册 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  3. windows / Linux 远程桌面访问全面总结 / 共享文件

    一般来说,ssh 是指无图形界面形式,是命令行的方式.  速度快. vnc 是的是图形界面形式.                            速度慢. ssh 方式登陆: 1.windows ...

  4. Mysql 没有nvl()函数,却有一个类似功能的函数ifnull()

    今天自己无聊写了看了一个查询需求随手写了一个sql语句,发现竟然不能运行,MySQL报[Err] 1305 - FUNCTION ceshi.nvl does not exist的错.才意识到自己写的 ...

  5. PE下挂载注册表文件然后清除系统托盘空白图标缓存

    清除了右下角通知栏图标缓存TrayNotify(否则会出现一堆空白图标)清除缓存批处理脚本.bat如何在PE系统环境下清除宿主系统的托盘图标缓存? 清除了右下角通知栏图标缓存TrayNotify(否则 ...

  6. MII、GMII、RMII、SGMII、XGMII、XAUI、Interlaken

    MII即媒体独立接口,也叫介质无关接口.它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口,以及一个MAC和PHY之间的管理接口(图1).数据接口包括分别用于发送器和接收器的两条独立信道 ...

  7. 4.const

    const 放在*号的左边为指针常量,即:该指针所指向的内存空间不允许被修改.const放在*号的右边为常量指针,即:该指针的指向不允许被修改. 简单的说就是: 假设定义一个结构体 Teacher : ...

  8. 外部jar包 @Service 无法注解无法扫描问题

    在确保spring 配置 无误的情况下,如: <context:component-scan base-package="your.can.pakeage"></ ...

  9. python基础-初识Python和不同语言之间的区别

    一.Python的创始人谁? Python之父:吉多·范罗苏姆GuidovanRossum 吉多·范罗苏姆是一名荷兰计算机程序员,他作为Python程序设计语言的作者而为人们熟知.在Python社区, ...

  10. Perl/C#连接Oracle/SQL Server和简单操作

    连接数据库是一个很常见也很必须的操作.先将我用到的总结一下. 1. Perl 连接数据库 Perl 连接数据库的思路都是: 1)使用DBI模块: 2)创建数据库连接句柄dbh: 3)利用dbh创建语句 ...