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

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
  2. 广度优先搜索
  3. #include <iostream>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cmath>
  7. #include <string>
  8. #include <stack>
  9. #include <queue>
  10. #include <vector>
  11. #include <algorithm>
  12. using namespace std;
  13. const int Max=1100000;
  14. struct Point
  15. {
  16.     int x;
  17.     int num;
  18. };
  19. int N,K;
  20. bool vis[210000];
  21. void BFS()
  22. {
  23.     queue<Point>a;
  24.     Point s,t;
  25.     memset(vis,false,sizeof(vis));
  26.     s.num=0;
  27.     s.x=N;
  28.     a.push(s);
  29.     vis[N]=true;
  30.     while(!a.empty())
  31.     {
  32.         s=a.front();
  33.         a.pop();
  34.         if(s.x==K)
  35.         {
  36.             printf("%d\n",s.num);
  37.             return ;
  38.         }
  39.         if(!vis[s.x+1]&&s.x+1<=K*2)
  40.         {
  41.             t.x=s.x+1;
  42.             t.num=s.num+1;
  43.             a.push(t);
  44.             vis[t.x]=true;
  45.         }
  46.         if(s.x-1>=0&&!vis[s.x-1])
  47.         {
  48.             t.x=s.x-1;
  49.             t.num=s.num+1;
  50.             a.push(t);
  51.             vis[t.x]=true;
  52.         }
  53.         if(s.x*2<=K*2&&!vis[s.x*2])
  54.         {
  55.             t.x=s.x*2;
  56.             t.num=s.num+1;
  57.             a.push(t);
  58.             vis[t.x]=true;
  59.         }
  60.     }
  61. }
  62. int main()
  63. {
  64.     scanf("%d %d",&N,&K);
  65.     BFS();
  66.     return 0;
  67. }
  68.  

版权声明:本文为博主原创文章,未经博主允许不得转载。

Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏的更多相关文章

  1. Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...

  2. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏

    #include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...

  4. Can you find it? 分类: 二分查找 2015-06-10 19:55 5人阅读 评论(0) 收藏

    Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...

  5. Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

  6. 菊花加载第三方--MBprogressHUD 分类: ios技术 2015-02-05 19:21 120人阅读 评论(0) 收藏

    上次说到了网络请求AFN,那么我们在网络请求的时候,等待期间,为了让用户不认为是卡死或程序出错,一般都会放一个菊花加载,系统有一个菊花加载类叫UIProgressHUD.但是我今天要说的是一个替代它的 ...

  7. Rebuild my Ubuntu 分类: ubuntu shell 2014-11-08 18:23 193人阅读 评论(0) 收藏

    全盘格式化,重装了Ubuntu和Windows,记录一下重新配置Ubuntu过程. //build-essential sudo apt-get install build-essential sud ...

  8. iOS开发网络数据之AFNetworking使用 分类: ios技术 2015-04-03 16:35 105人阅读 评论(0) 收藏

    http网络库是集XML解析,Json解析,网络图片下载,plist解析,数据流请求操作,上传,下载,缓存等网络众多功能于一身的强大的类库.最新版本支持session,xctool单元测试.网络获取数 ...

  9. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

随机推荐

  1. PostgreSQL表空间

    postgres=# \h create tablespace Command: CREATE TABLESPACEDescription: define a new tablespaceSyntax ...

  2. Lintcode: Wood Cut

    Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you ...

  3. !!常见的上穿突破M20方式——突破还是试探的判断

    1和2相似之处在于M5<M20, 最大的区别是M20和M5之间的间距在放大还是缩小,如果是放大,大盘会先试探一下.如果越缩越小,大盘必须选择方向 但是必须注意的是,即使是夹角缩小,选在方向不一定 ...

  4. Facebook Hacker Cup 2014 Qualification Round

    2014 Qualification Round Solutions 2013年11月25日下午 1:34 ...最简单的一题又有bug...自以为是真是很厉害! 1. Square Detector ...

  5. 【IOS】2.基础

    1.Identifers命名规则 Identifers is combined with letters, underline, dollars, numbers must begin with le ...

  6. 。。。无语的Eclipse+Tomact。。。

    晕哦,今天又被Eclipse给骗了,今天部署了一个SSH的环境,搞了半天,JAR包是通过BuildPath导入进去的,怎么搞都报错,说是找不到Spring-Web的一个Jar包,差点没有把我给弄死.. ...

  7. RelativeLayout相对布局属性

    RelativeLayout用到的一些重要的属性: 第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中android:layout_center ...

  8. SparkSQL基础应用(1.3.1)

    一.概述 从1.3版本开始Spark SQL不再是测试版本,之前使用的SchemaRDD重命名为DataFrame,统一了Java和ScalaAPI. SparkSQL是Spark框架中处理结构化数据 ...

  9. mysql 管理工具

    摘自: http://www.chinaz.com/free/2009/0306/68691.shtml MySQL是一个非常流行的小型关系型数据库管理系统,2008年1月16号被Sun公司收购.目前 ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON OverpaintRegion1

    zw版[转发·台湾nvp系列Delphi例程]HALCON OverpaintRegion1 unit Unit1;interfaceuses Windows, Messages, SysUtils, ...