问题描述:

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.

解题思路:

bfs,三个方向搜索,x=x+1,x=x-1,x=x*2,最先搜索到的就是用时最短的。

代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
int n,k;
int a[]; struct node
{
int x;
int step;
}now,net; int bfs(int x)
{
queue<node> q;
now.x=x;
now.step=;
q.push(now);
while(q.size())
{
now=q.front();
q.pop();
//三种情况
if(now.x==k)return now.step;
net.x=now.x+;
if(net.x>=&&net.x<=&&a[net.x]==)
{
a[net.x]=;
net.step=now.step+;
q.push(net);
}
net.x=now.x-;
if(net.x>=&&net.x<=&&a[net.x]==)
{
a[net.x]=;
net.step=now.step+;
q.push(net);
}
net.x=now.x*;
if(net.x>=&&net.x<=&&a[net.x]==)
{
a[net.x]=;
net.step=now.step+;
q.push(net);
}
}
return -;
} int main()
{
while(~scanf("%d%d",&n,&k))
{
memset(a,,sizeof(a));
a[n]=;
int ans=bfs(n);
printf("%d\n",ans);
}
return ;
}

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

  1. Catch That Cow(广搜)

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

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

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

  3. HDU2717 Catch That Cow 【广搜】

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

  4. Catch That Cow 经典广搜

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

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

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

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

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

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

  8. BFS广搜题目(转载)

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

  9. 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. (三)ajax请求不同源之jsonp跨域

    凡是拥有"src"这个属性的标签都具有跨域的能力,比如<script>.<img>.<iframe>. JS中,我们直接用XMLHttpRequ ...

  2. NOIP2011 D2T3 观光公交 做题笔记

    目录 归纳题目的性质 算法 60分 100分 code 大家来找茬 总结 归纳题目的性质 每一个加速器效果相同(1) 车子等到所有人上车之后才会发车, 这个最早发车时间不由加速器的配比决定(2) 要优 ...

  3. java 解析富文本处理 img 标签

    很多项目都需要到富文本来添加内容,就好比新闻啊,旅游景点之类的,都需要使用富文本去添加数据,然而怎么我这边就发现了两个问题 1)怎样将富文本的图片的 src 获取出来? 2)后台上传的时候用的是相对路 ...

  4. Flex核心属性整理

    main axis和cross axis的位置不一定是水平和垂直的,以flex-direction的值即为主轴方向 justify-content:主轴对齐方式 space-between:将多余空间 ...

  5. 搭建自己的Webpack项目

    五,搭建自己的Webpack项目  https://www.jianshu.com/p/42e11515c10f

  6. centos7安装python3 以及tab补全功能

    1.安装python3 1.1下载python源码包 网址:https://www.python.org/downloads/release/python-362/ 下载地址:https://www. ...

  7. Spring-day02

    Annotation复习:1,Annotation:作为类型的元数据; 1,给类型加标记; 2,annotation可以添加各种类型的属性;2,Annotation的上的标记: 1),target:标 ...

  8. c语言的二进制表示的是什么码

    int -1 的二进制是 1111 1111 1111 1111 1111 1111 1111 1111 int -2 的二进制是 1111 1111 1111 1111 1111 1111 1111 ...

  9. 邮局 100分代码(dfs+多重剪枝)

    蓝桥杯真题-邮局 #include<iostream> #include<algorithm> #include<set> #include<string&g ...

  10. 免费为网站加上HTTPS

    前言 最近有好几位同学直接微信赞助说快点更新文章.这个要和大家说声抱歉,的确很久没有写文章了.我们也不找借口,我会尽力保证多写文章.今天我们的主题来讲解 如何给自己的网站 加上HTTPS HTTPS是 ...