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

    [root@wx03 0724]# cat a2.pl use Encode; my $a=<STDIN>; my $b=encode_utf8('微信'); print "\$ ...

  2. Poj 2255 Tree Recovery(二叉搜索树)

    题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...

  3. Button的设置及各种属性

    (1)UIButton类继承自UIControl,而UIControl继承自UIView,因为UIView就是个矩形区域,所以UIButton实例化的对象其实都是一个矩形,虽然有各种圆角.增加联系人. ...

  4. Object 保存到文件中

    6月4日 Object 保存到文件中  Q. 你添加一个新类到你的项目当中且你希望可以保存这个类的一个实例对象到磁盘文件 并在需要时从磁盘文件读回到内存中  A. 方案  确保你的类遵循 NSCodi ...

  5. VC++网络高级编程

    内含<VC网络高级编程>电子书 及源代码. 第一章.TCP/IP协议.第二章.Winsock网络编程接口:第二章.Visual C++与网络编程:第四章.基本网络编程技术:第五章.Teln ...

  6. 一个简单的mfc单页界面文件读写程序(MFC 程序入口和执行流程)

    参考:MFC 程序入口和执行流程  http://www.cnblogs.com/liuweilinlin/archive/2012/08/16/2643272.html 程序MFCFlie      ...

  7. 安装Tomcat指定JDK(转)

    一.应用实例 一般情况下一台服务器只跑一个业务,那么就直接配置一套环境,设置好Java环境变量即可.某些时候一台服务器上会安装多个业务,而且各个业务需要的JDK版本各不相同,或者为了使业务独立开来,需 ...

  8. php 解析url 和parse_url使用

    通过url进行传值,是php中一个传值的重要手段.所以我们要经常对url里面所带的参数进行解析,如果我们知道了url传递参数名称,例如 /index.php?name=tank&sex=1#t ...

  9. Install TightVNC Server in RHEL/CentOS and Fedora to Access Remote Desktops

    Virtual Networking Computing (VNC) is a Kind of remote sharing system that makes it possible to take ...

  10. C++成员变量与函数内存分配

    关于结构体和C++类的内存地址问题 C++类是由结构体发展得来的,所以他们的成员变量(C语言的结构体只有成员变量)的内存分配机制是一样的.下面我们以类来说明问题,如果类的问题通了,结构体也也就没问题啦 ...