Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 88732   Accepted: 27795

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.

Source

题目链接:http://poj.org/problem?id=3278
题意:有一个农民和一头牛,他们在一个数轴上,牛在k位置保持不动,农户开始时在n位置。设农户当前在M位置,每次移动时有三种选择:1.移动到M-1;2.移动到M+1位置;3.移动到M*2的位置。问最少移动多少次可以移动到牛所在的位置。所以可以用广搜来搜索这三个状态,直到搜索到牛所在的位置。
分析:BFS的板子题,用队列做,第一次学队列,感觉自己好弱啊!现在才学,算法真的好难学,学了又怕不会用,现在不敢学太快了!
下面每一步代码我都给出了详细解释,一看就懂!
 #include<iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include<queue>
#define MAX 100001
using namespace std;
queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
bool visit[MAX];//访问空间
int step[MAX]; //记录步数的数组不能少
bool bound(int num)//定义边界函数
{
if(num<||num>)
return true;
return false;
}
int BFS(int st,int end)
{
queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
int t,temp;
q.push(st);//进队列
visit[st]=true;
while(!q.empty())//重复使用时,用这个初始化
{
t=q.front();//得到队首的值
q.pop();//出队列,弹出队列的第一个元素,并不会返回元素的值
for(int i=;i<;++i) //三个方向搜索
{
if(i==)
temp=t+;
else if(i==)
temp=t-;
else
temp=t*;
if(bound(temp)) //越界
continue;
if(!visit[temp])//访问空间未被标记
{
step[temp]=step[t]+;
if(temp==end)
return step[temp];
visit[temp]=true;//标记此点
q.push(temp);//将temp元素接到队列的末端;
}
}
}
}
int main()
{
int st,end;
while(scanf("%d%d",&st,&end)!=EOF)
{
memset(visit,false,sizeof(visit));//visit数组进行初始化操作
if(st>=end)
cout<<st-end<<endl;
else
cout<<BFS(st,end)<<endl;
}
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: 46715   Accepted: 14673 ...

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

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

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

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

  4. 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 ...

  5. 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 ...

  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. 每周.NET前沿技术文章摘要(2017-06-07)

    汇总国外.NET社区相关文章,覆盖.NET ,ASP.NET等内容: .NET .NET Core and .NET Framework Working Together, Or: The Magic ...

  2. CET——4 常用短语

    在网上看到的,先拔到自己这来,四级大大,求过!!!!

  3. Button的五种点击事件

    1.内部类方式 class MyOnClickListener implements View.OnClickListener{ /** * Called when a view has been c ...

  4. LAMP第四部分mysql操作

    1. 忘记root密码编辑mysql主配置文件 my.cnf 在[mysqld]字段下添加参数  skip-grant  ,重启数据库服务,这样就可以进入数据库不用授权了 mysql -uroot , ...

  5. Cleaner, more elegant, and wrong(翻译)

    Cleaner,more elegant,and wrong 整洁,更优雅,但是错的 并不是因为你看不到错误的产生路径就意味着它不存在. 下面是C#编程书中的一个片段,摘自关于异常处理的章节. try ...

  6. JMeter常见错误解决方法

    1.Windows 平台,双击 jmeter/bin 目录下 jmeter.bat 文件,jmeter 无法启动且报错如下: 此问题是没有配置 jdk 环境变量所致,配置好 jdk 环境变量即可. 2 ...

  7. pycharm2017.3专业版激活注册码

    pycharm作为一个不错的python编程的ide很有用处 这里拔出一段专业版的注册码,社区版用起来确实着实让人着急. 2017-12-1921:40:38 EB101IWSWD-eyJsaWNlb ...

  8. 当你的电脑出现stop: 0X0000007B

    这几天可算是把我折腾惨了.先是linux系统无法进入图形化桌面,几经折腾,我把linux删除重装.怎知道,我在瘟都死下删除linux的分区,结果我的两个瘟都死分区也没了,哭了我去恢复数据,但是然并卵. ...

  9. vim编辑器的使用技巧

    vim(vi)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率.vi是“visual interface”的缩 ...

  10. Ubuntu 安装MyEclipse10

    Ubuntu 安装MyEclipse10 1.安装JDK 2.下载myeclipse.run 3.  现在假设你的 myeclipse.run 的路径是/home/yourname/myeclipse ...