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 ...
随机推荐
- 原生js 操作类名
添加类名: document.getElementById('navBar').getElementsByClassName('mui-tab-item')[0].classList.add('mui ...
- WP8学习笔记:如何在页面显示前自动转向到其他页面
在本次修练开始之前,我们除了预设的 MainPage页面外,也另外新增了一个 Login页面,如下图示: MainPage.xaml页面长这样 Login.xaml页面长这样 因为我们的需求是要求使用 ...
- centos7 安装及破解 jira 7.3.3
JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域. 同样jira 的运行依赖java环境,上一节已经 ...
- ViewStub 的使用
一.内容概述 举例说明ViewStub标签的使用 二.ViewStub类的文档说明及应用场举例 文档描述: A ViewStub is an invisible, zero-sized View th ...
- go项目布局(摘录)
go的项目结构布局 或 包结构布局 这一块大家似乎还在摸索吧, 常用的应该还是类似于java的mvc布局, 但网上也有不同的布局方式,查阅github上的一些源码,也有大量的采用. 我把自己碰到的资料 ...
- redis实践一些要注意的事项
不要放垃圾数据,及时清理无用数据实验性的数据和下线的业务数据及时删除; key尽量都设置过期时间对具有时效性的key设置过期时间,通过redis自身的过期key清理策略来降低过期key对于内存的占用, ...
- Centos使用光盘作为本地yum源
[root@localhost CentOS]# mkdir /media/CentOS把光盘加载到本地[root@localhost CentOS]# mount /dev/cdrom /media ...
- MySql(一):linux 安装mysql数据库——yum安装法
mysql数据库有多种安装方式,本文只介绍在Linux服务器上最实用.最快捷的mysql server安装方法.一.Linux服务器yum安装(CentOS6.3 64位)所有在服务器上执行的命令,都 ...
- Sql语句查询XML - 小结
--两种方式查询 DECLARE @varXML XML, @varXML1 XML --.xml数据源为属性方式 SET @varXML = '<PARAM> <Row FID = ...
- docker的使用02
自定义容器名称: docker run --name -i -t ubuntu /bin/bash docker ps -a 数据卷的使用 数据卷其实就是容器和宿主机目录之间的映射. 具体实现: su ...