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

  1. 5 17

Sample Output

  1. 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所需的最小步数。
  1. #include <cstdio>
  2. #include <queue>
  3. #define MAX 100001
  4. using namespace std;
  5. int n, m;
  6. int ret[MAX], vis[MAX] = {0};
  7. queue<int> q;
  8. int bfs(int n, int m)
  9. {
  10. if(n == m)
  11. return 0;
  12. int cur;
  13. q.push(n);
  14. while(!q.empty())
  15. {
  16. cur = q.front();
  17. q.pop();
  18. if(cur + 1 < MAX && !vis[cur+1])
  19. {
  20. ret[cur+1] = ret[cur] + 1;
  21. vis[cur+1] = 1;
  22. q.push(cur+1);
  23. }
  24. if(cur + 1 == m)
  25. break;
  26. if(cur - 1 >= 0 && !vis[cur-1])
  27. {
  28. ret[cur-1] = ret[cur]+1;
  29. vis[cur-1] = 1;
  30. q.push(cur-1);
  31. }
  32. if(cur - 1 == m)
  33. break;
  34. if(cur * 2 < MAX && !vis[cur*2])
  35. {
  36. ret[cur*2] = ret[cur] + 1;
  37. vis[cur*2] = 1;
  38. q.push(cur*2);
  39. }
  40. if(cur * 2 == m)
  41. break;
  42. }
  43. return ret[m];
  44. }
  45. int main()
  46. {
  47. scanf("%d%d", &n, &m);
  48. printf("%d\n", bfs(n, m));
  49. return 0;
  50. }

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. 快排 quicksort 快速排序

    首先看这个 http://www.cnblogs.com/morewindows/archive/2011/08/13/2137415.html 看完后就没有什么不理解的了... PS 注意 凡是在一 ...

  2. Office 2013

    Microsoft Office 2013 Professional Plus 32位简体中文版文件名: SW_DVD5_Office_Professional_Plus_2013_W32_ChnSi ...

  3. 打印NxN的矩阵

    找出规律,并打印出一个NxN的矩阵,规律就是从首坐标开始顺时针依次增大: #include<iostream> #include<vector> using namespace ...

  4. SQLServer 2008 :error 40 出现连接错误

      在与SQLServer建立连接时出现与网络相关的或特定与实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且SQL SERVER已配置允许远程链接.(provide:命名管道提供程序,e ...

  5. Android设置Activity背景为透明style

    方法一: 通过Theme.Translucent @android:style/Theme.Translucent @android:style/Theme.Translucent.NoTitleBa ...

  6. 优化器的使用oracle ---explain plan

    如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQL的每一步执行是否存在问题. 如果一条SQL平时执行的好好的,却有一天突然性能很差,如果排除了系统资源和阻塞的原因,那么基本可以 ...

  7. ASP.NET -- repeater控件的使用

    转载自网络-原网址[http://blog.csdn.net/haitaodoit/article/details/7021214] repeater绑定数据: protected void Page ...

  8. 我的IOS学习之路(三):手势识别器

    在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等.这里做一下总结,详见代码. - (void)viewDidLoad { [super viewDidLo ...

  9. WPF 实现的等待效果界面

    这个界面的效果是从WinForm 转变过来,可以实现等待的效果,操作完成以后就自动关掉. 效果图如下 有一个动态的手机等待效果的样式,中间的文字可以自己定义,提供了方法可以修改中间"正在加载 ...

  10. QT中QWidget、QDialog及QMainWindow的区别

    本文转自http://www.cnblogs.com/aqxin/archive/2011/05/23/2054156.html QWidget类是所有用户界面对象的基类. 窗口部件是用户界面的一个基 ...