Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 36079   Accepted: 11123

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 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?

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.

【题目来源】

【题目大意】
在x轴的正半轴上,一个人的出发点是N,一头牛在另一点K,人可以有两种操作:
1.步行:+1 or -1
2.传送:*2
问你通过最少的步数到达牛的位置,需要多少步。
 
【题目分析】
就是一个裸的广搜,使用队列实现。
 
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int N,K; struct now
{
int x;
int step;
};
bool vis[]; int BFS(int x)
{
int tx;
now ans,tp;
queue<now> que;
ans.x=x,ans.step=;
vis[x]=true;
que.push(ans);
while(!que.empty())
{
tp=que.front();
que.pop();
tx=tp.x+; //no.1
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
tx=tp.x-; //no.2
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
tx=tp.x*; //no.3
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
}
} int main()
{
while(cin>>N>>K)
{
if(N==K)
{
cout<<""<<endl;
continue;
}
memset(vis,false,sizeof(vis));
cout<<BFS(N)<<endl;
}
return ;
}

当然还可以剪枝一下,比如说:如果人的坐标大于牛的坐标,这时就只需要做-1这一步就可以了。

BFS --- 模板题的更多相关文章

  1. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  2. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  3. hdu1242 又又又是逃离迷宫(bfs模板题)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...

  4. Saving Princess claire_(hdu 4308 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...

  5. Knight Moves(hdu1372 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)   ...

  6. 用一道模板题理解多源广度优先搜索(bfs)

    题目: //多元广度优先搜索(bfs)模板题详细注释题解(c++)class Solution { int cnt; //新鲜橘子个数 int dis[10][10]; //距离 int dir_x[ ...

  7. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  8. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  9. AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. python高级编程——线程和线程池

    线程模块           线程的特点:                本质上是异步的.需要多个并发活动.每个活动的处理顺序可能是不确定的.或者说是随机的,不可预测的,宏观上是同时运行的       ...

  2. css的尺寸、display的属性、以及浮动和清除浮动的方法

    css的尺寸width heightline-height 行高是由三部分构成,上间距 文本高度 下间距,且上下间距相等.所以文字居中.行高:一旦设置了行高,元素内部必须有内容.line-height ...

  3. QT场景视图父子关系图元打印研究

    在之前的一篇文章中,实现了QT场景视图的打印功能,主要通过render函数来实现,非常简单和方便. 在实际的项目需求中,除了打印整个场景外,还需要对单个图形进行打印操作,基于item的图形可以在pai ...

  4. MySQL单表最大记录数不能超过多少?

    MySQL单表最大记录数不能超过多少? 很多人困惑这个问题.其实,MySQL本身并没有对单表最大记录数进行限制,这个数值取决于你的操作系统对单个文件的限制本身. 从性能角度来讲,MySQL单表数据不要 ...

  5. 【转载】Gradle学习 第十一章:使用Gradle命令行

    转载地址:http://ask.android-studio.org/?/article/94 This chapter introduces the basics of the Gradle com ...

  6. 1 Ubuntu 破解帐户密码

    1.  重启Ubuntu  长按shift进入grub界面 2.  选择recovery mode模式,进入Recovery Menu界面选择 root        Drop to root she ...

  7. JS引擎是如何工作的?从调用堆栈到Promise

    摘要: 理解 JS 引擎运行原理. 作者:前端小智 原文:JS引擎:它们是如何工作的?从调用堆栈到Promise,需要知道的所有内容 Fundebug经授权转载,版权归原作者所有. 为了保证可读性,本 ...

  8. 利用 FluentScheduler 启动定时器计划任务

    FluentScheduler 是什么? Automated job scheduler with fluent interface. 这是作者在 Github 上的介绍,就是一个定时任务管理器.在 ...

  9. 《linux就该这么学》课堂笔记17 分离解析、DHCP、电子邮件系统

    1.动态主机配置协议(DHCP,Dynamic Host Configuration Protocol) 自动管理局域网内主机的IP地址.子网掩码.网关地址及DNS地址等参数,可以有效地提升IP地址的 ...

  10. php静态化介绍

    1.动态URL地址设置成静态形式http://state.com/index.php?c=play&a=index&id=16267 ------>http://state.co ...