Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 88732   Accepted: 27795

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 X - 1 or X + 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.

Source

题目链接:http://poj.org/problem?id=3278
题意:有一个农民和一头牛,他们在一个数轴上,牛在k位置保持不动,农户开始时在n位置。设农户当前在M位置,每次移动时有三种选择:1.移动到M-1;2.移动到M+1位置;3.移动到M*2的位置。问最少移动多少次可以移动到牛所在的位置。所以可以用广搜来搜索这三个状态,直到搜索到牛所在的位置。
分析:BFS的板子题,用队列做,第一次学队列,感觉自己好弱啊!现在才学,算法真的好难学,学了又怕不会用,现在不敢学太快了!
下面每一步代码我都给出了详细解释,一看就懂!
  1. #include<iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include<queue>
  6. #define MAX 100001
  7. using namespace std;
  8. queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
  9. bool visit[MAX];//访问空间
  10. int step[MAX]; //记录步数的数组不能少
  11. bool bound(int num)//定义边界函数
  12. {
  13. if(num<||num>)
  14. return true;
  15. return false;
  16. }
  17. int BFS(int st,int end)
  18. {
  19. queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
  20. int t,temp;
  21. q.push(st);//进队列
  22. visit[st]=true;
  23. while(!q.empty())//重复使用时,用这个初始化
  24. {
  25. t=q.front();//得到队首的值
  26. q.pop();//出队列,弹出队列的第一个元素,并不会返回元素的值
  27. for(int i=;i<;++i) //三个方向搜索
  28. {
  29. if(i==)
  30. temp=t+;
  31. else if(i==)
  32. temp=t-;
  33. else
  34. temp=t*;
  35. if(bound(temp)) //越界
  36. continue;
  37. if(!visit[temp])//访问空间未被标记
  38. {
  39. step[temp]=step[t]+;
  40. if(temp==end)
  41. return step[temp];
  42. visit[temp]=true;//标记此点
  43. q.push(temp);//将temp元素接到队列的末端;
  44. }
  45. }
  46. }
  47. }
  48. int main()
  49. {
  50. int st,end;
  51. while(scanf("%d%d",&st,&end)!=EOF)
  52. {
  53. memset(visit,false,sizeof(visit));//visit数组进行初始化操作
  54. if(st>=end)
  55. cout<<st-end<<endl;
  56. else
  57. cout<<BFS(st,end)<<endl;
  58. }
  59. return ;
  60. }

POJ 3278 Catch That Cow(BFS,板子题)的更多相关文章

  1. poj 3278 Catch That Cow (bfs搜索)

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

  2. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  3. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  4. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

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

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

  7. POJ 3278 Catch That Cow bfs 难度:1

    http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...

  8. POJ - 3278 Catch That Cow bfs 线性

    #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...

  9. BFS POJ 3278 Catch That Cow

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

随机推荐

  1. 【java】System成员输入输出功能out、in、err

    package System输入输出; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOu ...

  2. iOS 获取当前应用的信息以及用户信息:版本号手机号手机型号

    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...

  3. APP安全--网络传输安全 AES/RSA/ECC/MD5/SHA

    移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...

  4. xml生成方式二(Xml序列化器XmlSerializer)

    一.andoirdAPI提供了xml生成和解析的API: XmlSerializer xs = Xml.newSerializer();和XmlPullParser xmlPullParser = X ...

  5. bzoj 1566: [NOI2009]管道取珠

    Description   Input 第一行包含两个整数n, m,分别表示上下两个管道中球的数目. 第二行为一个AB字符串,长度为n,表示上管道中从左到右球的类型.其中A表示浅色球,B表示深色球. ...

  6. badboy 录制脚本并并发脚本

    很久没有研究过接口相关的工具了,一个偶然的机会听说了 badboy,可以录制jemter脚本, 查了资料 还可以并发,于是乎,实践才知道. http://www.badboy.com.au/ 官网,我 ...

  7. python3.5 + PyQt5 +Eric6 实现的一个计算器

    目前可以实现简单的计算.计算前请重置,设计的时候默认数字是0,学了半天就做出来个这么个结果,bug不少. python3.5 + PyQt5 +Eric6 在windows7 32位系统可以完美运行 ...

  8. 百度图表插件echart简单应用,简单配置一些要显示的样式及种类

    从echart官网下载js,然后引入jq即可运行.足够简单应用了 关键词:echart控制:图标标题.数据标题.折线图.柱状图切换按钮.恢复刷新图表按钮.保存为图片按钮.坐标系控制.坐标数据.坐标倾斜 ...

  9. [编织消息框架][网络IO模型]NIO(select and poll)

    上面测试论证系统内核在read data时会阻塞,如果我们在把第一个阶段解决掉那么性能就会提高 NIO 编程 JDK 1.4中的java.nio.*包中引入新的Java I/O库,其目的是提高速度.实 ...

  10. Vue 子组件无法使用 $emit 向父组件传参

    问题描述: