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. iOS-国家代码选择功能github开源分享

    三行代码集成国家区号选择功能 功能执行效果如图: 开源链接: https://github.com/qxuewei/XWCountryCode 用法: 1.导入XWCountryCode类 2.在须要 ...

  2. POJ Cow Exhibition

    题目链接:Click Here~ 题目意思自己看吧. 算法分析: 对我来想是没有想到,最后看别人的博客才知道的.要把当中的一个条件当作体积.由于两个条件都存在负数,所以还要先保证最后不会再体积中出现负 ...

  3. J2EE之EJB

     EJB是sun的JavaEEserver端组件模型,最大的用处是部署分布式应用程序.EJB把使用java开发的server组件的部署和开发进行标准化. 凭借java跨平台的优势.用EJB技术部署的分 ...

  4. 【Android】The application has stopped unexpectedly.Please try again.

    打出的android APK在android4.0.2手机上运行正常,但是在2.3.7的MOTO DEFY ME525上运行失败. android 2.3.3版本的模拟器上面运行失败: Sorry! ...

  5. MySQL解释--百度百科

    MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS ...

  6. 在ubuntu10.04 下将360wifi当无线网卡使用

    通过百度“360wifi linux“ 已经有很多解决方案.主要过程是从网上下载mt7601驱动包,编译出一个内核模块后,再通过modprobe 添加模块.下面描述的是基于我本机的特点所做的额外工作. ...

  7. 【Java】Java_04 HelloWorld提升

    HelloWorld的总结与提升 1.Java对大小写敏感,如果出现了大小写拼写错误,程序无法运行 2.关键字public被称作访问修饰符(access modifier),用于控制程序的其它部分对这 ...

  8. prototype 用法

    prototype使得js面向对象使用了prototype之后,使用它里面的属性或者函数 需要new出一个对象才可以使用.否则不使用prototype,直接向对象注入 function Person( ...

  9. 同时安装office2016与visio2016的实现过程

    visio 2016安装问题 同时安装office2016与visio2016的实现过程 visio2016  but failed

  10. oracle 数据查询

    1,读取从今天到1个月前之间的数据select * from tablewhere column between add_months(sysdate, -1) and sysdate;