hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6383 Accepted Submission(s): 2034
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
bool isw[];
int step[];
void bfs(int n,int k)
{
memset(isw,,sizeof(isw));
queue <int> q;
int cur,next;
cur = n;
step[n] = ;
isw[cur] = true;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur==k) //找到,返回结果
return ;
int i;
for(i=;i<=;i++){ //步行,或者传送
switch(i){
case :
next = cur - ;
if(isw[next]) //剪枝,走过的不能走
break;
if(next< || next>) //剪枝,越界不能再走
break;
step[next] = step[cur] + ;
q.push(next);
isw[next] = true;
break;
case :
next = cur + ;
if(isw[next])
break;
if(next< || next>)
break;
step[next] = step[cur] + ;
q.push(next);
isw[next] = true;
break;
case :
next = cur * ;
if(isw[next])
break;
if(next< || next>)
break;
step[next] = step[cur] +;
q.push(next);
isw[next] = true;
break;
}
}
}
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
bfs(n,k);
printf("%d\n",step[k]);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- Catch That Cow (BFS广搜)
问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
- HDU 2717 Catch That Cow (深搜)
题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- Catch That Cow(广搜)
个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John h ...
随机推荐
- js 给json添加新的字段,或者添加一组数据,在JS数组指定位置删除、插入、替换元素
JS定义了一个json数据var test={name:"name",age:"12"};需要给test再添加一个字段,需要什么办法,可以让test的值为{na ...
- Android 弹幕效果开发案例
概述 现在有个很流行的效果就是弹幕效果,满屏幕的文字从右到左飘来飘去.看的眼花缭乱,看起来还蛮cool的 现在就是来实现这一的一个效果,大部分的都是从右向左移动漂移,本文的效果中也支持从左向右的漂移移 ...
- 执行pig出错Unhandled internal error. Found interface jline.Terminal, but class was expected
执行pig时报例如以下错误 2015-07-14 10:41:12,869 [main] ERROR org.apache.pig.Main - ERROR 2998: Unhandled inter ...
- JS应用(资料很全)
http://www.cnblogs.com/meil/archive/2007/02/06/642559.html 如果你找的javascript的东西的话,建议你 ctrl+F 直接在这个页上找 ...
- 迭代器类vector::iterator 和 vector::reverse_iterator 的实现、迭代器类型、常用的容器成员
一.迭代器 迭代器是泛型指针 普通指针可以指向内存中的一个地址 迭代器可以指向容器中的一个位置 STL的每一个容器类模版中,都定义了一组对应的迭代器类.使用迭代器,算法函数可以访问容器中指定位置的元素 ...
- 【Unity】开发WebGL内存概念具体解释和遇到的问题
自增加unity WebGL平台以来.Unity的开发团队就一直致力于优化WebGL的内存消耗. 我们已经在Unity使用手冊上有对于WebGL内存管理的详尽分析,甚至在Unite Europe 20 ...
- [显示属性]-自定义桌面里没有IE选项
1楼 哈哈,我来告诉你原因,微软为了应对欧盟的反垄断调查,在 SP3 的“自定义桌面”里去掉了 Internet Explorer 选项. 如果桌面 IE 图标被误删除,但是又想恢复,而不是建立快捷方 ...
- 温故而知新 forEach 无法中断(break)的问题
forEach无法使用break和return来中断,只能使用throw catch来达到中断的效果了. var id = (function(){ // forEach 是无法中断的.除非用这种ha ...
- 雅虎天气API调用
雅虎天气API调用: 1.调用方法:http://weather.yahooapis.com/forecastrss?w=2502265&u=c,绿色字体为城市代号,u=c表示取摄氏度. 2. ...
- 11个常用的Linux命令
Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.今天为你解释下面几个命令:sudo.python.mtr.Ctrl+x+e.nl.s ...