博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 877D Olya and Energy Drinks(BFS+剪枝)
阅读量:6258 次
发布时间:2019-06-22

本文共 3068 字,大约阅读时间需要 10 分钟。

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 n, m 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.

Example

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的迷宫,可以沿着任一方向走K格,从起点到达终点最少需要多少步?

题解:

很容易想到BFS,但是直接BFS的话就TLE,需要剪枝,可以剪枝的是如果当前方向上的某个点访问过,并且到起点的距离大于当前点的距离,则该方向的就不用再搜索了。

#include
#include
#include
using namespace std;const int maxn=1005,INF=0x3f3f3f3f;char maze[maxn][maxn];int dx[]={1,-1,0,0},dy[]={0,0,1,-1};int d[maxn][maxn];bool vis[maxn][maxn];int n,m,k;int sx,sy,ex,ey;struct node{ int x,y,step;};void bfs(){ for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) d[i][j]=INF; node cur; cur.x=sx,cur.y=sy,cur.step=0; queue
que; que.push(cur); d[sx][sy]=0;//起点要设为0 while(que.size()) { cur=que.front(); que.pop(); for(int i=0;i<4;i++) for(int j=1;j<=k;j++) { node next; next.x=cur.x+dx[i]*j; next.y=cur.y+dy[i]*j; next.step=cur.step+1; if(next.x<1||next.x>n||next.y<1||next.y>m||maze[next.x][next.y]=='#'||(vis[next.x][next.y]&&cur.step>=d[next.x][next.y])) break;//如果当前方向上的某个点访问过,并且到起点的距离大于当前点的距离,则该方向的就不用再搜索了 if(next.step
>n>>m>>k; for(int i=1;i<=n;i++) cin>>(maze[i]+1); cin>>sx>>sy>>ex>>ey; bfs(); return 0;}

转载于:https://www.cnblogs.com/orion7/p/7782860.html

你可能感兴趣的文章
Android 系统API实现数据库的增删改查和SQLite3工具的使用
查看>>
95、Android下在onCreate中获取控件的宽度和高度(通过回调)
查看>>
UML在需求分析阶段的应用
查看>>
JavaScript:JavaScript事件的处理
查看>>
WEB安全测试的类型
查看>>
ES6笔记(7)-- Promise异步编程
查看>>
早睡早起
查看>>
C#软件监控外部程序运行状态
查看>>
几款开源的图形化Redis客户端管理软件推荐
查看>>
数据库设计中常见表结构的设计技巧
查看>>
CVPR论文《100+ Times Faster Weighted Median Filter (WMF)》的实现和解析(附源代码)。...
查看>>
MATLAB模糊逻辑(2)
查看>>
linux 内核模块管理
查看>>
【每日一摩斯】-【序列】-续-RAC and Sequences (853652.1)
查看>>
把一个select查询结果插入到一个表(可选指定字段和值实例)
查看>>
使用windbg抓取崩溃文件和分析的过程
查看>>
ViewHolder模式超简洁写法
查看>>
项目管理学习笔记之三.绩效分析
查看>>
php十行代码将xml转成数组
查看>>
centos 7 执行 groupinstall报错
查看>>