Olya and Energy Drinks(bfs)
2 seconds
256 megabytes
standard input
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.
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.
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.
3 4 4
....
###.
....
1 1 3 1
3
3 4 1
....
###.
....
1 1 3 1
8
2 2 1
.#
#.
1 1 2 2
-1
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)的更多相关文章
- cf 442 D. Olya and Energy Drinks
cf 442 D. Olya and Energy Drinks(bfs) 题意: 给一张\(n \times m(n <= 1000,m <= 1000)\)的地图 给出一个起点和终点, ...
- 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 ...
- 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 ...
- 【Codeforces Round #442 (Div. 2) D】Olya and Energy Drinks
[链接] 我是链接,点我呀:) [题意] 给一张二维点格图,其中有一些点可以走,一些不可以走,你每次可以走1..k步,问你起点到终点的最短路. [题解] 不能之前访问过那个点就不访问了.->即k ...
- 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 ...
- Codeforces Round #442 Div.2 A B C D E
A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...
- [转]Speeding Up Websites With YSlow
本文转自:http://net.tutsplus.com/tutorials/other/speeding-up-websites-with-yslow/ We all know there are ...
- 洛谷P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler
P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler 题目描述 Farmer John has purchased the world's most l ...
- 使用现代C++如何避免bugs(下)
使用现代C++如何避免bugs(下) About virtual functions Virtual functions hinder a potential problem: the thing ...
随机推荐
- Linux学习笔记 (四)归档和压缩
一.zip压缩命令: 1.压缩文件: 格式:zip 压缩文件 源文件 例:zip abc.zip abc //将abc文件压缩到abc.zip文件内. 2.压缩目录: 格式:zip –r 压缩目录 ...
- 万字总结:学习MySQL优化原理(转)
本文转自:https://www.tuicool.com/wx/2eMBfmq 前言 说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *.不使用NULL字段.合理创建索引. ...
- Zigbee事件
ZIGBEE事件有两类.系统定义事件和用户定义事件. 系统类事件是协议栈已定义好的.用户类事件是我们用户层面来定义的. 事件类号採用一个16bit的常量,使用独热码编码,独热码是仅仅有一个bit为1, ...
- html的小例子
常用的前端实例: 1略 2.在网页商城中的图片当我们把鼠标放上去之后,图片会显示一个有颜色的外边框,图片某一部分的字体的颜色并发生改变 鼠标放上去之前 鼠标放上去之后: 实现的代码: <!DOC ...
- Linux——解决RedHat6/CentOS6系统中“弹出界面eth0:设备似乎不存在”的问题
刚刚在自己的CentOS6系统中执行service network restart时,竟然提示: 弹出界面 eth0: 设备 似乎不存在, 初始化操作将被延迟. [失败] 这事可真神奇.于是手动编 ...
- hdu - 4782 - Beautiful Soup(模拟)
题意:输出一堆乱排版的html标签,去多余空字符,转换为按缩进输出. 题目链接:pid=4782">http://acm.hdu.edu.cn/showproblem.php?pid= ...
- Array,Vector,List,Deque的区别与联系【转+改】
数组 内存连续分配,长度大小固定,内置的最基础的数据结构之一.支持随机访问和随机存储. 该类型数据所占内存空间最小. Vector 是C++ STL中的一个容器.和数组类似,它拥有一段连续的内存空间, ...
- Date日期类型转化成中文字符串
例子: select to_char(sysdate,'yyyy"年"mm"月"dd"日"') as nowYear from dual 结 ...
- jmeter 之 BSF,BeanShell(转载)
jmeter无法自行处理javascript,但是它可以用自带的BSF PreProcessor(BSF:面向java的脚本语言,支持javascript) (使用这个之前要把bsh-2.0b2.ja ...
- 开源静态分析工具androguard体验
原文链接:http://blog.csdn.net/xbalien29/article/details/21885297 虽然在windows端免费版的IDA.VTS等工具都可用来静态分析,但相对来说 ...