Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points - 1 or + 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?

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

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<iostream>
#include<queue>
using namespace std;
struct point
{
int x;///记录位置
int count;///记录步数
};
queue<point>q;
struct point s,now,t;
int vis[];///假设FJ开始的位置就是100000,那么变化两倍之后就是200000
int bfs(int n,int m)
{
int j;
while(!q.empty())
{
q.pop();
}///清空队列
memset(vis,,sizeof(vis));
vis[s.x]=;
q.push(s);
while(!q.empty())
{
t=q.front();
if(t.x==m)
return t.count;
for(j=; j<; j++)
{
now=t;
if(j==)
{
now.x=now.x+;
}
else if (j==)
{
now.x=now.x-;
}
else if(j==)
{
now.x=now.x*;
}
now.count++;
if(now.x==m)
{
return now.count;
}
if(now.x>=&&now.x<=&&vis[now.x]==)
{
vis[now.x]=;
q.push(now);
}
}
q.pop();
}
return ;///二者开始的位置相同
}
int main()
{
int n,m,ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
s.x=n;
s.count=;
ans=bfs(n,m);
printf("%d\n",ans);
}
return ;
}

反思:这道题和之前的那一道剑客救公主那一道题一样,不仅仅需要考虑题意之中的搜索方式,还要考虑搜索不到或者起始位置与终止位置相同等特殊情况,该去如何设置被调函数,该去返回一个什么样的值,这两道题都是因为这一点使我wa了好多次,引以为戒。

Catch That Cow(BFS广搜)的更多相关文章

  1. Catch That Cow (BFS广搜)

    问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

  2. Catch That Cow(广搜)

    个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John h ...

  3. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

  4. HDU2717 Catch That Cow 【广搜】

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. Catch That Cow 经典广搜

    链接:http://poj.org/problem?id=3278 题目: Farmer John has been informed of the location of a fugitive co ...

  6. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  7. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. 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,最先 ...

  9. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

  10. hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. php与java

    作者:eechen链接:https://www.zhihu.com/question/20377398/answer/141328982来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...

  2. vue 项目 切换手机端和pc端。同一个项目,配置不同的路由

    1, 首先判断设备:在main.js里面写 // vue原型挂载 - 是否PC端 if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator. ...

  3. html5 video获取当前时间和视频总时间长度

    html: <video id="video-active" class="video-active" width="640" hei ...

  4. webpack4的react打包错误

    因为之前一直用的是脚手架创建项目,第一次自己学习创建webpack打包.loader我是复制别人的. module: { loaders: [ { test: /\.js?$/, exclude: / ...

  5. Spark 加载数据库mysql表中数据进行分析

    1.工程maven依赖包 <properties> <spark_version>2.3.1</spark_version> <!-- elasticsear ...

  6. django路由基本使用-6

    路由定义位置 django的路由是定义在 urls.py 文件下的 urlpatterns 列表中的. urls.py 文件是路由解析的入口. from django.conf.urls import ...

  7. 关于Json.dumos中的ensure_ascii

    在使用json,dumps时,当需要输出中文时,需要在后面添加  ensure_ascii = Fasle 因为json.dumps 序列化时,默认对中文使用的是 ascii 编码,添加后才能输出中文 ...

  8. Java基础——语法基础

    一.标识符 1.不能使用数字开头 2.不能使用关键字 (更多命名规范,参见基础加强随笔) 二.数据类型 主要分为四种: 整形: byte 1字节8位    范围 -128~127 short    2 ...

  9. asp.net微信公众平台本地调试设置

    1.首先要开启内网穿透功能,我这边使用自己搭建的ngrok内网穿透服务(具体如何搭建ngrok内网穿透服务,另开一篇说) 2.开启内网穿透后,即可实现互联网访问 www.tbkmama.cn 指向 1 ...

  10. 成都Uber优步司机奖励政策(2月18日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...