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. Django中间件执行顺序

    中间件 Django中的中间件是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出.中间件的设计为开发者提供了一种无侵入式的开发方式,增强了Django ...

  2. @Component注解、@Service注解、@Repository注解、@Controller注解区别

    --------------------------------------------------------------------------------------------------- ...

  3. vue 新属性学习

    1, $listeners 父级元素 <base-input v-on:focus.native="onFocus"></base-input> 子级元素 ...

  4. C语言自问自答

    Windows系统下,最好如何配置环境? notepad++,tdm-gcc,powershell来进行! scanf函数的返回值,和不符合格式如何返回? #include<stdio.h> ...

  5. 关于verilog中的signed类型

    在数字电路中,出于应用的需要,我们可以使用无符号数,即包括0及整数的集合:也可以使用有符号数,即包括0和正负数的集合.在更加复杂的系统中,也许这两种类型的数,我们都会用到. 有符号数通常以2的补码形式 ...

  6. 前端模拟API数据的两种方式

    第一种方法:使用 mock-api 1.创建一个项目 2.创建一个资源 3.拖动创建记录 点击data查看记录 4.如何访问API的数据 第一种方式:在终端中通过 curl + 地址,如图: 第二种方 ...

  7. xmind打开文件报错

    可以尝试使用导入 文件——导入 选择此方式打开即可.

  8. c#调用c++库函数

    如果是非托管的,就用DllImport,举例    using System;    using System.Runtime.InteropServices;    class MainApp    ...

  9. 北京Uber优步司机奖励政策(2月25日)

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

  10. CDN 缓存策略(转)

    1.CDN加速原理    通过动态域名解析,网友的请求被分配到离自己最快的服务器.CDN服务器直接返回缓存文件或通过专线代理原站的内容.    网络加速+内容缓存,有效提供访问速度 2.CDN节点数量 ...