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. 查看sqlserver 2008中性能低下的语句

    经常使用这个语句来查看性能低下的sql语句: SELECT creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' ,total_physical ...

  2. Linux下统计当前文件夹下的文件个数、目录个数(转)

    1) 统计当前文件夹下文件的个数 代码如下: ls -l |grep "^-"|wc -l 2) 统计当前文件夹下目录的个数 代码如下: ls -l |grep "^d& ...

  3. centos7 yum安装遇到报错:Head V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEYer

    centos7 yum安装时遇到错误:Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY 无法安装时,可按如下方法解决: This mini ...

  4. 记一次CurrentDirectory导致的问题

    现在项目里需要实现一个功能如下: A.exe把B.exe复制到临时目录,然后A.exe退出,B.exe负责把A.exe所在的整个目录删除. 实现: A.exe用CreateProcess创建B.exe ...

  5. 15-spring学习-集合表达式

    spring认为,数组和List集合是等价的. 要想操作list集合,利用“{内容,内容...}”的形式就可以完成了(类似数组). package com.Spring.ELDemo; import ...

  6. 纯css实现单行”截取“

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 5分钟教你配置命令行界面的163邮箱client

    网易163免费邮箱相关server信息: mutt 是命令行下的邮件client,它仅是管理邮件的组织编辑,邮件的发送和接收须要调用相应的程序. 这里用的各自是getmail 和 msmtp. 过程例 ...

  8. HTML5学习笔记 Geolocation(地理定位)

    HTML5 Geolocation(地理定位)用于定位用户的位置. 定位用户的位置 html5 Geolocation API用于获得用户的地理位置 鉴于该特性可能低侵犯用户的隐私,除非用户同意,否则 ...

  9. 帧率(FPS)计算的六种方法总结

    原文地址:http://blog.csdn.net/u012494876/article/details/53368164 帧率(FPS)计算是游戏编程中常见的一个话题.大体来说,总共有如下六种方法: ...

  10. mybatis-spring 项目简介

    mybatis-spring 主要是为了解决 spring3.0 中支持 mybatis3.0 1 mybatis-spring 主要包含以下几个部分 数据源 SqlSessionFactory Sq ...