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.
 
需求:有一个人现在的位置为n,有头牛现在的位置为k,
     这个人从位置n开始走,有两三种走法,前进一步,后退一步,或者跳到2*n,所需的时间都是1
      假设这头牛位置不变,问这个人追到这头牛所需的时间是多少
 
vis[i] = 0表示位置i没有走过,vis[i] =1表示位置i已经走过了
step[i]表示到达位置i所需要的部数
思路:宽度搜索,每步所有的走法按照特定的顺序依次入栈,先走到k所需的步数一定是最小的
    第一步位置:n
       第二步可能的位置:n+1, n-1, n*2;
       每次的位置x必须满足 0<= x <= 100000;
       按照可能的位置把所有的可能位置依次入栈(同时把这个位置标记为已经走过的),每次弹出栈顶元素,看从这个栈顶位置往下走每一步可能的位置,按照同样的方法把这些位置入栈     
       如果到达位置k,那么step[k]就是到达k所需的最小步数。
#include <cstdio>
#include <queue>
#define MAX 100001
using namespace std;
int n, m;
int ret[MAX], vis[MAX] = {0};
queue<int> q;
int bfs(int n, int m)
{
if(n == m)
return 0;
int cur;
q.push(n);
while(!q.empty())
{
cur = q.front();
q.pop();
if(cur + 1 < MAX && !vis[cur+1])
{
ret[cur+1] = ret[cur] + 1;
vis[cur+1] = 1;
q.push(cur+1);
}
if(cur + 1 == m)
break;
if(cur - 1 >= 0 && !vis[cur-1])
{
ret[cur-1] = ret[cur]+1;
vis[cur-1] = 1;
q.push(cur-1);
}
if(cur - 1 == m)
break;
if(cur * 2 < MAX && !vis[cur*2])
{
ret[cur*2] = ret[cur] + 1;
vis[cur*2] = 1;
q.push(cur*2);
}
if(cur * 2 == m)
break;
}
return ret[m];
}
int main()
{
scanf("%d%d", &n, &m);
printf("%d\n", bfs(n, m));
return 0;
}

E - Catch That Cow的更多相关文章

  1. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  2. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  3. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  4. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  5. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

  6. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  7. BFS POJ 3278 Catch That Cow

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

  8. Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 58072   Accepted: 18061 ...

  9. [HDOJ2717]Catch That Cow

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

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

随机推荐

  1. 树莓派高级GPIO库,wiringpi2 for python使用笔记(二)高精度计时、延时函数

    学过单片机的同学应该清楚,我们在编写传感器驱动时,需要用到高精度的定时器.延时等功能,wiringpi提供了一组函数来实现这些功能,这些函数分别是: micros() #返回当前的微秒数,这个数在调用 ...

  2. STL"源码"剖析

    STL"源码"剖析-重点知识总结   STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略 ...

  3. nefu 462 fib组合

    nefu 462 fib组合 (斐波那契数列的通项公式以及推倒过程) 分类: 数学2014-05-21 10:27 190人阅读 评论(0) 收藏 举报 题目链接:http://acm.nefu.ed ...

  4. java 动态获取web应用的部署路径

    public static String DEPLOY_PATH = null; static { String CurrentClassFilePath = Constant.class.getRe ...

  5. The Linux device model

    /sys和 /dev的疑问 1./dev 下放的是设备文件,是由应用层mknod创建的文件.假设底层驱动对mknod的设备号有相应的驱动,如open等函数.那么应用层open "/dev/* ...

  6. ceph之纠删码

    转自:http://m.blog.csdn.net/blog/skdkjxy/45695355 一.概述 按照误码控制的不同功能,可分为检错码.纠错码和纠删码等. 检错码仅具备识别错码功能 而无纠正错 ...

  7. ##DAY5 UIControl及其子类

    ##DAY5 UIControl及其子类 #pragma mark ———————UIControl——————————— UIControl初识: 1)UIControl是有控制功能的视图(比如UI ...

  8. ajax的get与post提交方式

    Get方式的用户名验证 1.编写html代码 <form> 用户名[GET]:<input id="usernameID" type="text&quo ...

  9. string模板

    string模块中包含了一个很有用的Template类,可以先写好字符串模板,后期使用的时候直接替换就可以了.         模板中使用$作为占位符前缀,使用{}包裹占位符以支持间断的标量名,使用$ ...

  10. 0520 python

    配置python环境变量我的电脑->右键->属性->高级系统设置->环境变量->(1)用户变量->新建 Path=C:\Python27(2)系统变量->编辑 ...