题目链接: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. c# base关键的理解

    base 最大的使用地方在面相对性开发的多态性上.(什么是多态,如何理解多态) base可以[完成]创建派生类实例时调用其基类构造函数或者调用基类上已经被其他方法重写的方法 //关于base调用基类构 ...

  2. Aggregation 聚集

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. 01-C语言概述

    本文目录 一.C语言简史 二.C语言的特点 三.C语言能做什么? 四.C语言的版本问题 五.C语言语法预览 回到顶部 一.C语言简史 C语言于1972年发明,首次使用是用于重写UINX操作系统(UNI ...

  4. 常用webservice接口

    商业和贸易: 1.股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) Endpoint: http://webservice.webxml.com.cn/WebSe ...

  5. mac 安装tomcat

    一.下载 首先在tomcat官网下载完整版的 tomcat.tar.gz包,Core下 注: zip用于windows操作系统,tar.gz用于unix和linux操作系统. Binary Distr ...

  6. ElasticSearch学习问题记录——Invalid shift value in prefixCoded bytes (is encoded value really an INT?)

    最近在做一个电商项目,其中商品搜索中出现一个奇怪的现象,根据某个字段排序的时候会出现商品数量减少的情况.按照一般路要么查不出来,要么正常显示,为什么增加了按照销量排序就会出现查询结果减少的情况. 查了 ...

  7. vim 简单配置

    在启动vim时,当前用户根目录下的.vimrc 文件会被自动读取,该文件可以包含一些设置甚至脚本,所以,一般情况下把.vimrc 文件创建在当前用户的根目录下比较方便,即:$vi ~/.vimrc,然 ...

  8. Caché数据库学习笔记(4)

    目录 DeepSee的使用 数据.方法等的导入与导出 ======================================================== ================ ...

  9. js判断IE浏览器版本

    if(navigator.userAgent.indexOf("MSIE")>0){ if(navigator.userAgent.indexOf("MSIE 6. ...

  10. windows下面安装casperjs

    因为需要 就学习了一下casperjs,CasperJS是一个开源的导航脚本处理和测试工具,基于PhantomJS(前端自动化测试工具)编写.由于casperjs对PhantomJS的依赖性,所以需要 ...