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. MySQl5.6最新安装

    http://www.cnblogs.com/xiongpq/p/3384681.html http://dev.mysql.com/doc/refman/5.5/en/source-configur ...

  2. When Is Cheryl's Birthday

    大早上起来逛微博,看见@西瓜大丸子汤Po的一个逻辑题,遂点开看之... 原文链接:http://nbviewer.ipython.org/url/norvig.com/ipython/Cheryl.i ...

  3. 关于ASP控件对象的一些简单操作

    在线人数 Application.Lock(); Application[).ToString(); Application.UnLock(); Label1.Text = Application[& ...

  4. eclipse或adt-bundle创建的android项目没有自动生成MainActivity.java和activity_main.xml等文件解决办法

    以前我电脑一直以来都是用的eclipse3.7来开发android项目的,创建android项目也能正常生成MainActivity.java和activity_main.xml等文件.后来不知道什么 ...

  5. jQuery相关面试题

    1 你在公司是怎么用jquery的? 答:在项目中是怎么用的是看看你有没有项目经验(根据自己的实际情况来回答) 你用过的选择器啊,复选框啊,表单啊,ajax啊,事件等 配置Jquery环境 下载jqu ...

  6. KMP算法番外篇--求解next数组

    KMP算法实现字符串的模式匹配的时间复杂度比朴素的模式匹配好很多,但是它时间效率的提高是有前提的,那就是:模式串的重复率很高,不然它的效率也不会凸显出来.在实际的应用中,KMP算法不算是使用率很高的一 ...

  7. SqlHelp

    using System.Configuration;using System.Data; public class SqlHelp { private static string connectio ...

  8. android sdk Manager path

  9. PHP学习笔记12-上传文件

    上传图片文件并在页面上显示出图片 enctype介绍:enctype属性指定将数据发回到服务器时浏览器使用的编码类型. 取值说明: multipart/form-data: 窗体数据被编码为一条消息, ...

  10. [LeetCode]题解(python):095-Unique Binary Search Trees II

    题目来源: https://leetcode.com/problems/unique-binary-search-trees-ii/ 题意分析: 给一个整数,返回所有中序遍历是1到n的树. 题目思路: ...