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. [安全学习环境]Win7 下DVWA安装指南

    一.环境依赖: .Net Framework 3.5 PHP+MySQL 集成测试环境:XAMPP V3.2.1 二.环境准备 1.下载XAMPP(http://www.wampserver.com/ ...

  2. onkeyup事件只能输入数字,字母,下划线

    <input type="text" onkeyup="value=value.replace(/[\W]/g,'')"/>

  3. 关于在mfc中cstring转为float和ini

    CString str1,str, str2; GetDlgItemText(IDC_EDIT1, str1); GetDlgItemText(IDC_EDIT2, str2); UINT value ...

  4. ubuntu开放防火墙端口

    root@jbxue:$ sudo ufw enable  Firewall started and enabled on system startup  root@jbxue:$ sudo ufw ...

  5. python的web开发环境Django配置

    我的系统的windows10: 第一步,安装python3.5 第二步,配置django,如图所示,在python的安装目录下的Scripts里面执行:pip install Django,我这儿提示 ...

  6. JavaScript DOM编程艺术-学习笔记(总结一)

    1.1)dom-core方法:(不专属于js,支持dom的任何一种程序设计语言都可以使用它,它们的用途,也不仅限于处理网页,也可以用来处理任何一种标记语言编写处理的文档)  ①getElementBy ...

  7. swift UILabel加载html源码

    @IBOutlet weak var content: UILabel! func setup(content:String){ self.content.preferredMaxLayoutWidt ...

  8. php 编程效率(2)

    1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍. 当然了,这个测试方法需要在十万级以上次执行,效果才明显. 其实静态方法和非静态方法的 ...

  9. C# lesson2

    一.C#数据类型 1.值类型 包括数据相关(short.long.int .double.float).布尔(bool).枚举 2.引用类型 Object .对象.数组.字符串 二.存储方式 值类型 ...

  10. MAC下安装automake autoconf工具

    I noticed today that while Mac OS 10.6 (specifically, 10.6.2) comes with automake and autoconf, the ...