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 ...
随机推荐
- 在ubuntu10.04 下将360wifi当无线网卡使用
通过百度“360wifi linux“ 已经有很多解决方案.主要过程是从网上下载mt7601驱动包,编译出一个内核模块后,再通过modprobe 添加模块.下面描述的是基于我本机的特点所做的额外工作. ...
- 未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral,解决
升级json.net版本时候报的错误 只需要解决.net和json版本冲突即可 <runtime> <assemblyBinding xmlns="urn:schemas- ...
- 设置U盘启动
利用快捷键来设置U盘启动,利用快捷键启动相对来说比较简单快捷,推荐大家使用(重要提醒:选择热键前,请先插入U盘) 组装机主板 品牌笔记本 品牌台式机 主板品牌 启动按键 笔记本品牌 启动按键 台式机品 ...
- 转:Window10下RabbitMQ安装图文教程
一.erlang下载安装 1.下载 下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang.org/downloads,双 ...
- java 中 HashMap 遍历与删除
HashMap的遍历 方法一.这是最常见的并且在大多数情况下也是最可取的遍历方式 /** * 在键值都需要时使用 */ Map<Integer, Integer> map = new Ha ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- 创建Mat
#include<opencv2/opencv.hpp> #include<iostream> #include<cassert> #include<vect ...
- 李洪强iOS开发之OC[003] - 用钥匙串存储信息模拟登陆
- jquery通过val()取不到textarea中的值
小编定义了一个textarea控件,却无法根据id取到textarea对象并赋值. 经过实验,得出了原因.代码如下: <!DOCTYPE html> <html> <he ...