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. NIM博弈的必胜取法

    #include<stdio.h> ; int a[Max]; int main() { int i,n,ans; int tmp; while(scanf("%d", ...

  2. python_ftplib实现通过FTP下载文件

    1.  Ftplib常用函数介绍 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,本次主要介绍连接FTP并且进行文件下载功能,可 ...

  3. AcmeAir安装AI探针--企业版

    通过脚本安装AI探针请点击通过脚本自动安装探针 一.安装企业版AI探针准备工作: 1. 准备好可用的docker版AcmeAir应用 2. 准备好可用的企业版AIServer 3. 下载好合适版本的J ...

  4. 让浏览器屏蔽js

    有时候为了测试js是否做到了渐进增强,需要屏蔽下js 做法: Internet选项>>安全>>自定义级别 禁用java小程序脚本和活动脚本 有的浏览器调试器直接就有这个功能

  5. FastDFS之java客户端使用

    为了方便应用程序的访问FastDFS,官网提供了fastdfs-client-java,以便更好的与应用程序结合使用. 下载fastdfs-client-java源码添加到项目工程里面,添加配置文件: ...

  6. 294. Flip Game II

    题目: You are playing the following Flip Game with your friend: Given a string that contains only thes ...

  7. redis 2.4异常

    最近公司redis服务出现了异常,记录下教训: redis异常后:观察redis服务,可以看到redis cpu占用100% 用strace命令查看redis进程,显示如下: open("/ ...

  8. dojo 资源库

    文档 :http://wenku.baidu.com/link?url=Nnek_tAjIC-Q3t3e9zHQmsh4LhU3f0ncC1QH8WD_U9-I8-fJ7mMisscFpgfuS8nU ...

  9. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

  10. poj - 1258 Agri-Net (最小生成树)

    http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...