Catch That Cow

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 12798 Accepted Submission(s): 3950

Problem 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

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.

Source
USACO 2007 Open Silver


解析:简单BFS。易知农夫走的范围不会超过2*k,超过2*k就得不到最小值。


```
#include
#include

const int MAXN = 100000+5;

int dis[2MAXN];

int q[2
MAXN];

int bfs(int n, int k)

{

memset(dis, -1, sizeof dis);

dis[n] = 0;

int l = 0, r = 0;

q[r++] = n;

while(l < r){

int h = q[l++];

if(h == k)

return dis[k];

int tmp = h-1;

if(tmp >= 0 && dis[tmp] == -1){

q[r++] = tmp;

dis[tmp] = dis[h]+1;

}

tmp = h+1;

if(tmp <= 2k && dis[tmp] == -1){

q[r++] = tmp;

dis[tmp] = dis[h]+1;

}

tmp = h
2;

if(tmp <= 2*k && dis[tmp] == -1){

q[r++] = tmp;

dis[tmp] = dis[h]+1;

}

}

}

int main()

{

int n, k;

while(~scanf("%d%d", &n, &k)){

int res = bfs(n, k);

printf("%d\n", res);

}

return 0;

}

HUD 2717 Catch That Cow的更多相关文章

  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. 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,最先 ...

  3. HDU 2717 Catch That Cow(常规bfs)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...

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

  5. hdoj 2717 Catch That Cow【bfs】

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

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

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

  7. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

  8. 杭电 HDU 2717 Catch That Cow

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

  9. Hdoj 2717.Catch That Cow 题解

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

随机推荐

  1. Ajax动态滚动加载数据

    看新浪微博,人人网都有这样的效果:滚动条滚动到最下面的时候,新的数据就被自动加载出来了,今天亲自尝试了一下这个效果的实现. 最开始在CSDN上写了一版,功能比较简单,今天又增加了一个小功能:翻页到指定 ...

  2. The absolute uri: http://struts.apache.org/tags-bean cannot be resolved in either web.xml or the jar files deployed with this application

    在一个tomcat中部署了一个struts-1.3.10的web项目,但是没有吧struts-1.3.10的lib中的jar包放进tomcat/lib中,所以导致了这个错误(访问该项目的页面时)

  3. VNC常用操作及常见问题解决办法汇总

    VNC登录用户缺省是root,但在安装oracle时必须用oracle用户的身份登录,下面我们就以oracle为例说明如何配置VNC,从而可以使用不同的用户登录到主机.步骤描述如下:    步骤一:修 ...

  4. HDFS dfsclient写文件过程 源码分析

    HDFS写入文件的重要概念 HDFS一个文件由多个block构成.HDFS在进行block读写的时候是以packet(默认每个packet为64K)为单位进行的.每一个packet由若干个chunk( ...

  5. 京东商城发现了一枚Bug

    我在京东上买了几本书,发现了一个BUG.. 买书的时候,我选了京东自营的书和京东其他店的书,合在一起购买,填写了开具发票. 然后,京东处理流程是,将上面一笔订单拆分成两笔,然后发票信息没有转到其他店那 ...

  6. Android技术面试整理

    前19题为常考题目!!  1.Android的四大组件是哪些,它们的作用? 2.请介绍下Android中常用的五种布局. 3.android中的动画有哪几类,它们的特点和区别是什么 4.android ...

  7. 51nod 1049 1049 最大子段和 (dp)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1049 令 dp[i]表示为以a[i]结尾的最大子段和,则  dp[i]= ...

  8. poj-2393 Yogurt factory (贪心)

    http://poj.org/problem?id=2393 奶牛们有一个工厂用来生产奶酪,接下来的N周时间里,在第i周生产1 单元的奶酪需要花费ci,同时它们也有一个储存室,奶酪放在那永远不会坏,并 ...

  9. IO(一)

    文件相关 package com.bjsxt.io.file; import java.io.File; /** * 两个常量 * 1.路径分隔符 ; * 2.名称分隔符 /(windows) /(l ...

  10. pyhton3多线程

    from time import ctime,sleep def music(): for i in range(2): print ("I was listening to music. ...