Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 37094   Accepted: 11466

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

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.
刚做的时候发生了很多奇异YY, 搞的我不知道重写了多少遍。。

从当前位置local,向local+1, local-1, local*2这三个方向扩展。
code:
#include <queue>
#include <cstdio>
#include<cstring>
#define N 100001
using namespace std;
int n, k, ans;
bool vis[N];
struct node {
int local, time;
};
void bfs() {
int t, i;
node now, tmp;
queue<node> q;
now.local = n;
now.time = 0;
q.push(now);
memset(vis,false,sizeof(vis));
vis[now.local] = true;
while(!q.empty()) {
now = q.front();
q.pop();
for(i=0; i<3; i++) {
if(0==i) t = now.local-1;
else if(1==i) t = now.local+1;
else if(2==i) t = now.local*2;
if(t<0||t>N||vis[t]) continue;
if(t==k) {
ans = now.time+1;
return;
}
vis[t] = true;
tmp.local = t;
tmp.time = now.time+1;
q.push(tmp);
}
}
} int main() {
while(~scanf("%d%d",&n,&k)) {
if(n>=k) printf("%d\n",n-k);
else {
bfs();
printf("%d\n",ans);
}
}
}

poj3278Catch That Cow(BFS)的更多相关文章

  1. 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,最先 ...

  2. poj3278-Catch That Cow 【bfs】

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  3. POJ3083Catch That Cow[BFS]

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77420   Accepted: 24457 ...

  4. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  5. POJ3278 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搜索)

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

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

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

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

  9. Catch That Cow (BFS广搜)

    问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

随机推荐

  1. 写一方法来实现两个变量的交换。在主调函数中定义两个整型变量,并初始化,调用交换方法,实现这两个变量的交换。(使用ref参数)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. C语言,调试必备的DEBUG宏定义

    1. #include <stdio.h> #include <stdarg.h> //仅仅是打印函数名字替换 DEBUG <--> printf #define ...

  3. FFT算法

    FFT算法的完整DSP实现 傅里叶变换或者FFT的理论参考: [1] http://www.dspguide.com/ch12/2.htm The Scientist and Engineer's G ...

  4. Android 开发 AirPlay Server

    安卓上开发  AirPlay Server  主要是参考了和修改了 DroidAirPlay项目 , 和Airplay 协议 1, 将DroidAirPlay 下载下来 2, Eclipse 新建一个 ...

  5. 理解iOS 8中的Self Sizing Cells和Dynamic Type

    http://www.cocoachina.com/ios/20140922/9717.html 在iOS 8中,苹果引入了UITableView的一项新功能--Self Sizing Cells,对 ...

  6. CCIE路由实验(3) -- BGP高级部分

    当一个AS包含多个IBGP对等体时,路由反射器非常有用.因为IBGP客户只需要和路由反射器建立邻居关系,从而降低了IBGP的连接数量.路由反射器和它的客户合称为一个簇.路由反射是克服IBGP水平分割的 ...

  7. C++的运算符

    C++的运算符十分丰富,使得C++的运算十分灵活方便.例如把赋值号(=)也作为运算符处理,这样,a=b=c=4就是合法的表达式,这是与其他语言不同的.C++提供了以下运算符: 算术运算符+(加)  - ...

  8. Android项目使用Assets下的文件

    Android项目在编译时,Assets下文件不被编译. Assets下的文件除了 html文件可以直接在项目中使用外,其他的文件都需要做处理滴. 在项目中使用方法:        使用流读取.   ...

  9. 01-Foundation简介、NSObject、copy、NSString

    目录: 一.Foundation常用类 二.Foundation简介 三.NSObject 四.NSString 回到顶部 一.Foundation常用类 1 NSObject.NSString.NS ...

  10. mysql Emoji表情字符集转换

    <pre name="code" class="html">Java代码 java.sql.SQLException: Incorrect stri ...