Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6383    Accepted Submission(s): 2034

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
 
Recommend
teddy   |   We have carefully selected several similar problems for you:  2102 1372 1240 1072 1728 

 
  bfs搜索题,基础题
  很简单的一道bfs搜索题,只不过把常见的二维地图换成了一维的,由于是生题,一开始想复杂了,注意剪枝,普通的广搜思路就能过。
  代码:

 #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
bool isw[];
int step[];
void bfs(int n,int k)
{
memset(isw,,sizeof(isw));
queue <int> q;
int cur,next;
cur = n;
step[n] = ;
isw[cur] = true;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur==k) //找到,返回结果
return ;
int i;
for(i=;i<=;i++){ //步行,或者传送
switch(i){
case :
next = cur - ;
if(isw[next]) //剪枝,走过的不能走
break;
if(next< || next>) //剪枝,越界不能再走
break;
step[next] = step[cur] + ;
q.push(next);
isw[next] = true;
break;
case :
next = cur + ;
if(isw[next])
break;
if(next< || next>)
break;
step[next] = step[cur] + ;
q.push(next);
isw[next] = true;
break;
case :
next = cur * ;
if(isw[next])
break;
if(next< || next>)
break;
step[next] = step[cur] +;
q.push(next);
isw[next] = true;
break;
}
}
}
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
bfs(n,k);
printf("%d\n",step[k]);
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)的更多相关文章

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

  2. Catch That Cow (BFS广搜)

    问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

  3. HDU 2717 Catch That Cow (深搜)

    题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...

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

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

  6. POJ3984 BFS广搜--入门题

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20816   Accepted: 12193 Descriptio ...

  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(BFS)

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

  9. Catch That Cow(广搜)

    个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John h ...

随机推荐

  1. DB中耗时的 存储过程 及执行详细情况

    SELECT a.object_id, a.database_id, OBJECT_NAME(object_id, database_id) 'proc name', a.cached_time, a ...

  2. CompletableFuture 详解

    转 http://www.jianshu.com/p/6f3ee90ab7d3 CompletableFuture类实现了CompletionStage和Future接口.Future是Java 5添 ...

  3. canves 图片旋转 demo

    <!DOCTYPE htmls> <html> <head> <title></title> <style> </styl ...

  4. yum groupinstall报错,处理方法

    http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/21/3089970.html ===== 创建repo库 # createrepo - ...

  5. Android 图片混排富文本编辑器控件

    概述 一个Android 图片混排富文本编辑器控件(仿兴趣部落) 详细 代码下载:http://www.demodashi.com/demo/12032.html 一.一个Android 图片混排富文 ...

  6. mybatis WARN No appenders could be found for logger的解决方法

    log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).log4j:WARN ...

  7. python 操作redis之——HyperLogLog (八)

    #coding:utf8 import redis # python 操作redis之——HyperLogLog r =redis.Redis(host=") # 1.Pfadd 命令将所有 ...

  8. Python 实int型和list相互转换 现把float型列表转换为int型列表 把列表中的数字由float转换为int型

    第一种方法:使用map方法 >>> list = [, ] #带有float型的列表 >>> int_list = map(int,list) #使用map转换 & ...

  9. location 禁止以/data开头的文件

    [root@web01 default]# tree data/ data/ └── index.html directories, file [root@web01 default]# cat da ...

  10. Google 商店:您的应用静态链接到的 OpenSSL 版本有多个安全漏洞。建议您尽快更新 OpenSSL

    安全提醒 您的应用静态链接到的 OpenSSL 版本有多个安全漏洞.建议您尽快更新 OpenSSL. 在开头为 1.0.1h.1.0.0m和 0.9.8za的 OpenSSL 版本中这些漏洞已得到修复 ...