题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717

Catch That Cow

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

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.

 
题目大意:在一条笔直的道路上,有一个农夫在A位置,一头牛在B位置,农夫一次可以搜索三个位置(例如农夫在x位置,他可以搜索x-1、x+1、x*2)问农夫至少需要几步能够找到牛。
解题思路: 用广搜 ,定义一个位置数组,每次搜索三个位置即可。
AC代码:
 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int f[]; //记录步数
void bfs(int n,int k)
{
int a[]; //位置数组
memset(f,,sizeof(f));
queue <int > q;
q.push(n);
f[q.front()] = ;
if (n == k)
return ;
while (!q.empty())
{
int t = q.front();
int x = f[t];
a[] = t-; //三个位置
a[] = t+;
a[] = t*;
for (int i = ; i < ; i ++)
{
if (a[i] >= && a[i] < && f[a[i]]==) //注意这里的边界值
{
q.push(a[i]);
f[a[i]] = x+; //在上一步的基础上加1
}
if (a[i] == k)
return ;
}
q.pop();
}
}
int main ()
{
int i;
int n,k;
while (~scanf("%d%d",&n,&k))
{
dfs(n,k);
printf("%d\n",f[k]-); //减去农夫本来在的位置那一步
}
return ;
}

HDU 2717 Catch That Cow (bfs)的更多相关文章

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

  2. 题解报告:hdu 2717 Catch That Cow(bfs)

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  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 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. hdu 2717 Catch That Cow(BFS,剪枝)

    题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...

  6. poj 3278(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. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  8. Catch That Cow(BFS)

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

  9. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

随机推荐

  1. java内部类以及匿名类

    内部类 一个类内部定义的类称为内部类. 内部类允许把逻辑相关的类组织在一起,并控制内部代码的可视性. 内部类与外部类的结构层次如下. 顶层类:最外层的类 外部类:内部类所在的类 内部类:类内部定义的类 ...

  2. lua table remove元素的问题

    当我在工作中使用lua进行开发时,发现在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: for key, value in pairs(tbtes ...

  3. KDE、GNOME 和 XFCE 桌面比较

    KDE.GNOME 和 XFCE 桌面比较   这么多年来,很多人一直都在他们的 linux 桌面端使用 KDE 或者 GNOME 桌面环境.在这两个桌面环境多年不断发展的同时,其它的桌面也在持续增加 ...

  4. WebSocket实战之————GatewayWorker使用笔记例子

    参考文档:http://www.workerman.net/gatewaydoc/ 目录结构 ├── Applications // 这里是所有开发者应用项目 │ └── YourApp // 其中一 ...

  5. PHP的autoload机制的实现解析

    在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都存放在一个单独的文件里,这样会很容易实现对类进行复用,同时将来维护时也很便利 一.autoload机制概述 在使用PHP的OO模式开发系 ...

  6. spring文件下载记录

    /** * 下载方法 * @param request * @param response * @param storeName 文件在存在位置的名字(需要带着后缀) * @param content ...

  7. Spring Boot工程发布到Docker

    先聊聊闲话 搞过企业级的application运维的同仁肯定深有感触,每个application的功能交叉错杂,数据交换就让人焦头烂额(当然这和顶层业务设计有关系), 几十个application发布 ...

  8. update

    update `表名` set 字段名 =replace(字段名, '查找的内容','更改的内容') where 字段名 like '%查找的内容%'; update shangpin set cli ...

  9. 在node.js中使用mongose模块

    对象与文档相对应 创建项目目录,用root进入 # mkdir /home/test/part9/ 直接# npm install mongoose,报错如下 ../node_modules/nan/ ...

  10. (41) Aeroo 模板设计基础教程

    1.     理论基础 注:我采用libreoffice5.2设计讲解 1.1.     定义模板指令 模板指令的语法和Genshi 模板语言相兼容,可以用Libreoffice( Write, Ca ...