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. SSIS -->> Data Type

    SSIS和SQL SERVER, .NET数据类型的映射表

  2. c++ 字符串函数用法举例

    1. substr() 2. replace() 例子:split() 字符串切割: substr 函数原型: , size_t n = npos ) const; 解释:抽取字符串中从pos(默认为 ...

  3. 修改linux默认jdk版本

    当你已经成功把jdk1.6.0_03 安装到 /usr/java,并且配置好了系统环境变量 执行 # java -version 时就是 显示jdk1.4.3,是因为你的linux系统有默认的jdk; ...

  4. 等宽格子堆砌 js

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 什么是智能dns解析

    智能DNS解析是针对目前电信和网通互联互通不畅的问题推出的一种DNS解决方案.具体实现是:把同样的域名如test.winiis.com的A记录分别设置指向网通和电信IP,当网通的客户访问时,智能DNS ...

  6. python2 和python3共存下问题

    一.使用python2 or python3 1. 使用python2 $ python xxx.py 2. 使用python3 $ python3 xxx.py 二.脚本调用 /usr/bin/en ...

  7. (4)FTP服务器下载文件

    上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...

  8. AOJ 2170 Marked Ancestor (基础并查集)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...

  9. poj 2096 Collecting Bugs (概率dp 天数期望)

    题目链接 题意: 一个人受雇于某公司要找出某个软件的bugs和subcomponents,这个软件一共有n个bugs和s个subcomponents,每次他都能同时随机发现1个bug和1个subcom ...

  10. Scrum Agile

    Scrum Agile 迭代式增量软件开发,敏捷开发,源于丰田汽车的制造流程. HMC测试流程: 1.hmc改配置 2.上ui验证 3.还原hmm(有的需要,有的不需要) 4.addReferal,在 ...