传送门: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): 20259    Accepted Submission(s): 5926

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
 
 
题目意思:
一维线性地图
人的位置在n,牛的位置在k
人每走一步有三种选择:+1,-1,*2
问你最少的步数是多少?
 
分析:
很普通的bfs,只有三种操作,如果三种操作后的位置都合法的话,就入队
 
需要注意的地方:
判断是否越界的时候,不能像题目中所说,
当牧场主所在的位置大于10W的时候,就认为他越界。
  因为他有可能先去到
100010的时候 ,在回来。所以再判断的时候,
因为就算了一开始站在10w的位置,你最多跳2倍,也最多到20w
所以
越界的最大值最好为20W。这样就不会出错了。
 
code:
#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
using namespace std;
#define max_v 100000
int vis[*max_v+];
int n,k;
struct node
{
int x,step;
};
int f(int x)//检查合法性
{
if(x<||x>=*max_v||vis[x]==)//超出范围或者用过
{
return ;
}
return ;
}
int bfs()
{
queue<node> q;
node p,next; p.x=n;
p.step=;
vis[n]=;
q.push(p); while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==k)
{
return p.step;
} //+1,-1,*2 三种情况都加入队列
next.x=p.x+;
if(f(next.x))
{
next.step=p.step+;
vis[next.x]=;
q.push(next);
} next.x=p.x-;
if(f(next.x))
{
next.step=p.step+;
vis[next.x]=;
q.push(next);
} next.x=p.x*;
if(f(next.x))
{
next.step=p.step+;
vis[next.x]=;
q.push(next);
}
}
return -;
}
int main()
{
int ans;
while(cin>>n>>k)
{
memset(vis,,sizeof(vis));
ans=bfs();
cout<<ans<<endl;
}
return ;
}
 

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

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

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

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

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

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

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

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

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

  6. 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,最先 ...

  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. hdoj 2717 Catch That Cow【bfs】

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

  9. 杭电 HDU 2717 Catch That Cow

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

随机推荐

  1. Java 基础(4)——常量 & 注释

    hello 呀,今天的内容超简单( ̄︶ ̄)↗并且,还有暗藏福利哟~~ 常量 常量 就是常常不变的量,第一次定义之后,就不会发生改变了.可能这就是 “常量” 的来源吧哈哈哈(玩笑). 一般来说,常量的定 ...

  2. 基于Node.js的ghost开源博客平台

    Ghost 是一套基于Node.js 构建的开源博客平台(Open source blogging platform),具有易用的书写界面和体验. 1.安装node windows 下安装npm:ht ...

  3. JBPM学习第2篇:为Eclipse添加JBPM开发支持

    1.Eclipse添加JBoss支持插件 参考:Eclipse添加JBoss支持 若已安装,直接跳过! 2.Eclipse添加Drools插件 jbpm-installer-full解压后的文件夹中找 ...

  4. Java BeanUtils 组件 使用

    1. BeanUtils组件 1.1 简介 程序中对javabean的操作很频繁, 所以apache提供了一套开源的api,方便对javabean的操作!即BeanUtils组件. BeanUtils ...

  5. 005hystrix.stream信息聚合Turbine

    1.POM配置 和普通Spring Boot工程相比,仅仅添加了Turbine和Spring Boot Starter Actuator依赖 <dependencies> <!--添 ...

  6. Git连接GitLab远程仓库

    1.简介 远程仓库是指托管在网络上的项目仓库,现在互联网上有很多项目托管平台,比如github.gitlab等.为了不公开自己项目代码,可以在自己的服务器上搭建自己的项目仓库,最常见的是搭建GitLa ...

  7. Spring Boot Async异步执行

    异步调用就是不用等待结果的返回就执行后面的逻辑,同步调用则需要等带结果再执行后面的逻辑. 通常我们使用异步操作都会去创建一个线程执行一段逻辑,然后把这个线程丢到线程池中去执行,代码如下: Execut ...

  8. android测试Code

    <!--android:layout_alignParentTop="true"--><com.koooke.platform.View.CenterImage ...

  9. Linux 虚拟机中配置 GNOME + VNC

    需求描述 在特定的需求下,需要用到 Linux 的图形化界面,但是 Azure 平台提供的虚拟机默认没有开放远程图形化登陆的功能.以下解决方案,提供了市面上非常流行的 GNOME + VNC 的组合来 ...

  10. Javascript 删除tr 元素

    Javascript 删除tr 元素   function delete1(obj){ var tr=obj.parentNode.parentNode; var tbody=tr.parentNod ...