链接:http://poj.org/problem?id=3278

题目:

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.
 
 
翻译:(俺英语不行,只好看翻译了)
题意:看完中文,题意就一目了然了,意思就是农夫追牛,牛不动,农夫有三种移动方式,
第一种:前移一步;
第二种:后移一步;
第二种:农夫现在的位置X2;
每次移动都消耗1分钟,求农夫追到牛的最短时间。
 
思路:又是一道搜索题,我们的目标是求出农夫抓住牛的最短时间,根据我们所学的两种搜索dfs与bfs,
我觉得bfs是浪费空间节省时间,dfs是浪费时间节省空间,
所以对于这种最短路径的问题,我们使用bfs显得更加快捷。
代码:
#include <cstdio>
#include<queue>
#include<cstring>
#define position 100005
using namespace std;
int n,k;
struct num//定义结构体表示每一个位置
{
int x;//位置坐标
int step;//走到这一步的步数(我们将时间转换为步数理解,每一步需要一分钟)
};
bool visit[position];//标记,判断是否走过
void bfs()
{
queue<num> steps;//定义队列存储位置
num start,now,next;//start=起点,now=前的位置,next=下一步的位置
memset(visit,false,sizeof(visit));//初始化标记(false表示未走过)
start.x=n;//初始化起点
start.step=0;
steps.push(start);//起点入队
visit[start.x]=true;//标记起点(表示走过)
while(!steps.empty())
{
now=steps.front();//取队列首元素表示当前位置
steps.pop();//首元素出列
if(now.x==k)//如果当前位置就是牛的位置(得到结果)输出步数,退出调用函数
{
printf("%d\n",now.step);
return;
}
for(int i=0;i<3;i++)
{
if(i==0)
next.x=now.x+1;
else if(i==1)
next.x=now.x-1;
else if(i==2)
next.x=now.x*2;//对三种方法进行判定
if(next.x>=0&&next.x<position&&!visit[next.x])
{
visit[next.x]=true;//表示走过了
next.step=now.step+1;//步数加一
steps.push(next);//入队
}
}
}
}
int main()
{
scanf("%d %d",&n,&k);
bfs();
return 0;
}

注意范围

Catch That Cow 经典广搜的更多相关文章

  1. Catch That Cow(广搜)

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

  2. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

  3. HDU2717 Catch That Cow 【广搜】

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

  4. Catch That Cow (BFS广搜)

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

  5. HDU 2717 Catch That Cow (深搜)

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

  6. poj 3278 Catch That Cow 优化深搜

    这题的思想很简单,就是每次找出队列里面花费时间最少的来走下一步,这样当我们找到k点后,所花费的时间一定是最少的. 但要用一个标记数组vis[200010],用来标记是否走过.否则会内存溢出. #inc ...

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

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

  8. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  9. Catch That Cow(BFS广搜)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

随机推荐

  1. 01_Keil与Proteus联合仿真的注意事项

    01. 关于keil5和Proteus8的联合仿真的操作步骤,这里就不细说,给个链接,步骤差不多是最齐全的 CSDN博客链接:https://blog.csdn.net/wzk456/article/ ...

  2. Systemd Journald占用资源过多

    journald占用过多磁盘空间 方法一 检查当前journal使用磁盘量 journalctl --disk-usage 清理方法可以采用按照日期清理,或者按照允许保留的容量清理,只保存2天的日志, ...

  3. Git 系列教程(3)- 初次运行 Git 前的配置

    前言 直接搬官网教程,再修改下,先啰嗦可以直接看以前的文章 Window初始化Git环境 https://www.cnblogs.com/poloyy/p/12185132.html Linux初始化 ...

  4. Mybatis源码解析5—— 接口代理

    本篇文章,可乐将为大家介绍通过接口代理的方式去执行SQL操作.话不多说,直接上图: 其实无论哪种方式,我们最终是需要找到对应的 SQL 语句,接口代理的方式就是通过 [包名.方法名] 的方式,去找到 ...

  5. Java一般命名规范

    一.项目名称 最好用英文,所有单词全部用小写,如testjavaproject.studentmanagement等,当然也也可以用中文,如"学生管理系统"等. 二.Java pr ...

  6. python安装easyinstall/pip出错

    在Windows中装了python3.6,自然还要装pip.按度娘的提供的方法先下载easyinstall,然后在CMD下输入: python ez_setup.py 结果报错 ........... ...

  7. 入坑微信小程序必经之路(六)图片上传服务器——WebSercice接口

    wxml文件 <view class="weui-uploader"> <view class="img-v weui-uploader__bd&quo ...

  8. Java基础系列(6)- 注释

    注释 平时我们编写代码,在代码量比较少的时候,我们还可以看懂自己写的,但是当项目结构一旦复杂起来,我们就需要用到注释了 注释不会被执行,是给开发人员看的 书写注释是一个非常好的习惯 Java中的注释有 ...

  9. python学习笔记(十一)-python程序目录工程化

    在一个程序当中,一般都会包含文件夹:bin.conf.lib.data.logs,以及readme文件. 所写程序存放到各自的文件夹中,如何进行串联? 首先,通过导入文件导入模块方式,引用其他人写好的 ...

  10. 总结了下PHPExcel官方读取的几个例子

    1.使用 PHPExcel_IOFactory 读取文件 $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 2.使用一个特定的读取类,读 ...