题目:

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?InputLine 1: Two space-separated integers: N and KOutputLine 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

  1. 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.
  2.  
  3. 题意:
    人抓牛,数轴上人在点N处,牛在点K处,通过前进1,后退1,当前位置乘以2三种方式到达K处,牛在K处不动,求人从N处到牛的K处所经过的最少步数;
  4.  
  5. 分析:
    求最小路径,用BFS
    分为+1,-1,*2三种情况;
    分为NK之前或者在K上的情况和NK之后的情况,NK之前就只能通过-1的操作到达K
    定义一个数组用于标记经过的位置坐标;
    定义一个数组用于记录到达当前位置所需要的最少步数;
    在三种情况下,满足范围即入队列,对当前的坐标进行标记,步数加1
  6.  
  7. AC代码:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. using namespace std;
  6. const int Max=;
  7. int n,k;
  8. int f[Max];
  9. int step[Max];
  10. bool cmp(int x)
  11. {
  12. return (x>=&&x<=Max);
  13. }
  14. int a,b;
  15. void bfs()
  16. {
  17. queue<int>s;
  18. s.push(n);
  19. f[n]=;
  20. step[n]=;
  21. while (!s.empty())
  22. {
  23. a=s.front();
  24. s.pop();
  25. if (a==k)
  26. break;
  27. b=a-;
  28. if (cmp(b)&&!f[b])
  29. {
  30.  
  31. s.push(b);
  32. f[b]=;
  33. step[b]=step[a]+;
  34. }
  35. b=a+;
  36. if (cmp(b)&&!f[b])
  37. {
  38. s.push(b);
  39. f[b]=;
  40. step[b]=step[a]+;
  41. }
  42. b=a*;
  43. if (cmp(b)&&!f[b])
  44. {
  45. s.push(b);
  46. f[b]=;
  47. step[b]=step[a]+;
  48. }
  49.  
  50. }
  51. }
  52. int main()
  53. {
  54.  
  55. while (scanf("%d %d",&n,&k)==)
  56. {
  57. memset(f,,sizeof(f));
  58. if (n>=k)
  59. printf("%d\n",n-k);
  60. else
  61. {
  62. bfs() ;
  63. printf("%d\n",step[k]);
  64. }
  65. }
  66.  
  67. return ;
  68. }
  1.  
  1.  
  2.  

Catch That Cow (BFS)的更多相关文章

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

  2. POJ 3278 Catch That Cow(bfs)

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

  3. HDU 2717 Catch That Cow(BFS)

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

  4. Catch That Cow(BFS)

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

  5. ***参考Catch That Cow(BFS)

    Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  6. Catch That Cow (bfs)

    Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...

  7. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

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

  8. 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)

    Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...

  9. poj 3278(hdu 2717) Catch That Cow(bfs)

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

  10. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

随机推荐

  1. 关于Weblogic的知识点

    一.解决Weblogic域创建.启动.进入控制台慢问题 搭建Weblogic 11g和12c环境时发现,安装正常,以默认组件安装,但是创建域的时候特别慢,一般需要几分钟至10分钟,卡在“创建域安全信息 ...

  2. Myeclipse 10/2014 配置插件(svn、maven、properties、velocity)的方法

    一.配置SVN详细图解 什么是SVN? 管理软件开发过程中的版本控制工具. 下面会以两种方式来介绍怎么安装svn,myeclipse安装SVN插件步骤,以myeclipse 2014为例,第一种是最常 ...

  3. NSPredicate 应用

    //查询单词里面包含“ang”的字符串 NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"sha ...

  4. Linux基础篇七:Linux的命令执行

    首选区分内置命令和外置命令: 内置命令:shell程序自带的命令,系统内核一启动就可以使用的命令 外置命令:在系统PATH变量路径下的命令 如何查看一个命令是内置命令还是外置命令: type -a c ...

  5. day21-双下eq方法

    class Goods: def __init__(self,name): self.name = name def __eq__(self,other): #self = apple1, other ...

  6. 常见字体图标库——font-awesome

    1.简介 FontAwesome一种带有网页功能的象形文字语言,并收集在一个集合里.字库中有675个图标,只支持英文搜索,中文地址:http://www.fontawesome.com.cn/ 2.使 ...

  7. tomcat端口占用异常

    错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009) 2011年01月18日 01:34:00 阅读数:202700 启动Tomcat服务器报错: ...

  8. Socket设置超时时间

    主要有以下两种方式,我们来看一下方式1: Socket s=new Socket(); s.connect(new InetSocketAddress(host,port),10000); 方式2: ...

  9. 关于使用MyEclipse自动生成Hibernate和Struts出现的jar不兼容的问题(antlr.collections.AST.getLine()I)

    今天做Hibernate和Struts2结合的练习,使用MyEclipse自动创建Hibernate和Struts的相关配置文件和jar包,写完一个查询方法后,准备测试一下结果,简单的查询那种,很诡异 ...

  10. 吴裕雄--天生自然python学习笔记:爬取我国 1990 年到 2017年 GDP 数据并绘图显示

    绘制图形所需的数据源通常是不固定的,比如,有时我们会需要从网页抓取, 也可能需从文件或数据库中获取. 利用抓取网页数据技术,把我国 1990 年到 2016 年的 GDP 数据抓取出来 ,再利用 Ma ...