题目链接: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. iOS - Frame 项目架构

    前言 iOS 常见的几种架构: 标签式 Tab Menu 列表式 List Menu 抽屉式 Drawer 瀑布式 Waterfall 跳板式 Springborad 陈列馆式 Gallery 旋转木 ...

  2. python 中date datetime time 与str的互转

    以下全部引入 form datetime import datetime, timedelta import time 一.time 转str 二.datetime 转 str str_date = ...

  3. golang csv问题

    go语言自带的有csv文件读取模块,看起来好像不错,今天玩玩,也算是系统学习go语言的一部分--^_^ 一.写csv文件 函数: func NewWriter(w io.Writer) *Writer ...

  4. akka笔记

    Actor UntypedActor actor的基类,继承并实现onReceive方法就可以得到一个Actor. Props 配置类,用Props.create可以创建一个按指定配置生成的Actor ...

  5. ANSI C与GNU C

    GNU计划,又称革奴计划,是由Richard Stallman在1983年9月27日公开发起的.它的目标是创建一套完全自由的操作系统.它在编写linux的时候自己制作了一个标准成为 GNU C标准.A ...

  6. 用cxf开发restful风格的WebService

    我们都知道cxf还可以开发restful风格的webService,下面是利用maven+spring4+cxf搭建webService服务端和客户端Demo 1.pom.xml <projec ...

  7. unity htc vive使用

    本文介绍如何在Unity中使用HTC vive设备,当前VR作为市场比较火热的热点,HTC VIVE设备作为三大供应商之一,许多人购买了该设备,却不知道如何使用,本文通过图文并茂的形式,进行手把手的讲 ...

  8. python——初识socket

    什么是socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链 ...

  9. C 风格字符串和strcpy方法的实现

    C语言是面向过程的,所以它并没有所谓封装好的功能强大的string.但是麻雀虽小五脏俱全.在C中,我们一般用 const char* 类型来定义一个字面型字符串. 首先我们了解下C中的基本指针.指针是 ...

  10. SuperSlide

    今天又接触到另一个新的前端插件,心情美美哒~ SuperSlide 致力于解决网站大部分特效展示问题,使网站代码规范整洁,方便维护更新,官网地址:http://www.superslide2.com/ ...