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

//简单bfs
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring> using namespace std;
int visit[];
int n,k; struct node
{
int x,step;
}; void bfs()
{
node st,ed;
queue <node> q;
st.x=n;
st.step=;
memset(visit,,sizeof(visit));
visit[n]=;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==k)
{
cout<<st.step<<endl;
return ;
}
if(st.x+<=&&visit[st.x+]==)
{
visit[st.x+]=;
ed.x=st.x+;
ed.step=st.step+;
q.push(ed);
}
if(st.x->=&&visit[st.x-]==)
{
visit[st.x-]=;
ed.step=st.step+;
ed.x=st.x-;
q.push(ed);
}
if(*st.x<=&&visit[*st.x]==)
{
visit[*st.x]=;
ed.step=st.step+;
ed.x=st.x*;
q.push(ed);
}
}
} int main()
{
while(cin>>n>>k)
{
bfs();
}
return ;
}

poj 3278 Catch That Cow bfs的更多相关文章

  1. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  2. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  3. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  4. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  5. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  6. poj 3278 Catch That Cow(bfs+队列)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  7. POJ 3278 Catch That Cow bfs 难度:1

    http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...

  8. POJ - 3278 Catch That Cow bfs 线性

    #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...

  9. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

随机推荐

  1. [Jenkins]怎么删除jenkins里项目配置的svn记录

    问题出现原因: 当持续集成项目运行一段时间之后,你会发现不知不觉中,配置了不少过往的SVN账号,都已经不能用了,失效了,影响选择有时候,想要删除 问题解决办法: 1.选择$JENKINS_HOME/c ...

  2. fpga之显示字符串

    //必须在有效区域下显示颜色才有颜色 显示字符可以在设定一个有效区域内显示 另加两个wire 求出新的x,ymodule vga_fpga( clk,rst_n, vga_b,vga_g,vga_r, ...

  3. cdh 安装记录

    安装文件准备 CDH 下载地址:http://archive.cloudera.com/cdh5/parcels/latest/ 下载操作系统对应的版本: 1.CDH-5.3.0-1.cdh5.3.0 ...

  4. jdk 多版本安装 for mac

    2016年mac上已经安装有jdk1.6的版本  目录在/Library/Java/JavaVirtualMachines/1.6.0.jdk 有时候mac版本跟新会自动删除jdk1.6 所以要去ma ...

  5. 视频编辑类sdk--lansoeditor--更新啦, 完全免费,欢迎下载

    当前版本是20160506 beta, 增加了兼容高通的64位硬件编码器增加MediaInfo类,您可以轻松获取多媒体中的各种信息.增加了演示的15个ffmpeg处理方法,您可以用这些方法实现秒拍图像 ...

  6. 使用Maven快速创建一个SpringMVC工程步骤

    第一步:创建maven工程,加入SpringMVC的maven依赖: <dependency> <groupId>org.springframework</groupId ...

  7. c++设计模式-----抽象工厂模式

    抽象工厂模式 要创建一组相关或者相互依赖的对象 作用:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. UML类图 抽象基类: 1)AbstractProductA.Abstrac ...

  8. 利用requestjs优化响应式移动端js加载

    html: <script data-main="main" src="require.js"></script> main.js re ...

  9. 使用ajax和history.pushState无刷新改变页面URL(转)

    表现 如果你使用chrome或者firefox等浏览器访问本博客.github.com.plus.google.com等网站时,细心的你会发现页面之间的点击是通过ajax异步请求的,同时页面的URL发 ...

  10. 判断iPhone设备是carplay和iPod的方法

    Carplay 参考代码: int UdevIsCarplay(int nVid, int nPid)//int UsbModeSwitch(pUdevStatus pStatus) { ; if ( ...