问题描述:

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. Codeforces.871D.Paths(莫比乌斯反演 根号分治)

    题目链接 \(Description\) 给定\(n\),表示有一张\(n\)个点的无向图,两个点\(x,y\)之间有权值为\(1\)的边当且仅当\(\gcd(x,y)\neq1\).求\(1\sim ...

  2. 浅析uWSGI、uwsgi、wsgi

    WSGI协议 首先弄清下面几个概念: WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述web ...

  3. angular.identity()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. SSIS获得Excel行号(转自http://blog.csdn.net/zplume/article/details/19113911)

    问题描述: 首先个人并不推荐将Excel作为数据源,因为Excel单元格式会引起特别多的数据转换问题,例如:单元格里明明是2.89,但SSIS抽取到数据库里面之后却变成了2.88999999之类的数据 ...

  5. scheduling while atomic和bad: scheduling from the idle thread(转)

    https://blog.csdn.net/shanzhizi/article/details/22949121 https://blog.csdn.net/wwwtovvv/article/deta ...

  6. C++标准库algorithm

    (1) 基本数学相关: max(t1, t2)和min(t1, t2), 返回t1和t2中的较大.较小者. max_element(b, e)和min_element(b, e), 返回两个迭代器所指 ...

  7. JavaWeb的学习路线

    JavaWeb的学习路线 一.BS结构 B/S结构(Browser/Server,浏览器/服务器模式),是WEB兴起后的一种网络结构模式,WEB浏览器是客户端最主要的应用软件.这种模式统一了客户端,将 ...

  8. c++11 强类型枚举 enum class

    在标准C++中,枚举类型不是类型安全的.枚举类型被视为整数,这使得两种不同的枚举类型之间可以进行比较.C++03 唯一提供的安全机制是一个整数或一个枚举型值不能隐式转换到另一个枚举别型. 此外,枚举所 ...

  9. Java代码实现封装多级树结构对象

    前言: 在开发中,我们经常见到,前端展示树状结构的,这时候就需要后端去封装一个多级树结构对象,前端根据这样结构的数据去渲染数据,这篇文章讲的是如何封装成多级树结构对象. 正文: 1.先封装个树结构的对 ...

  10. 2018-2019-2 网络对抗技术 20165236 Exp2 后门原理与实践

    2018-2019-2 网络对抗技术 20165236 Exp2 后门原理与实践 一.实验内容 (3.5分) (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用soc ...