传送门: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. linux 添加开机自启动脚本

    原文 Linux设置服务开机自动启动的方式有好多种,这里介绍一下通过chkconfig命令添加脚本为开机自动启动的方法. 1. 编写脚本autostart.sh(这里以开机启动redis服务为例),脚 ...

  2. 第2章 css边框属性

    圆角效果 border-radius border-radius是向元素添加圆角边框. 使用方法: border-radius:10px; /* 所有角都使用半径为10px的圆角 */ border- ...

  3. [转]纯js导出json到excel(支持chrome)

    转自:http://blog.csdn.net/educast/article/details/52775559 function JSONToExcelConvertor(JSONData, Fil ...

  4. 01.php面向对象

    下面是php_oop的一些基本知识 <?php //echo "<meta charset='utf-8'>" //设置中文输出 //1.面向对象类的建立: cl ...

  5. 核心API

    1.ProcessEngine ProcessEngine是Activiti中最核心的类,其他的类都是由他而来.Activiti流程引擎的配置文件是名为 activiti.cfg.xml 的XML文件 ...

  6. EF单实对应多表

    一.单实体对应多表 适用场景主表,拥有相同主键附属表或扩展表. 1. 建表词句 CREATE TABLE [Chapter2].[Product]( [SKU] [int] primary key , ...

  7. scp 上传和下载文件

    mac和linux自带scp命令,windows的话请考虑gitbash或者专业linux工具 (默认端口为22,所以-p可不写) 上传文件 scp -p /home/lnmp.gz root@ip: ...

  8. 微软发布Azure Stack第一个技术预览版

    为了提升商业灵敏度和加快创新步伐,各个企业都在迅速地转向云服务.在微软,我们已经见到微软智能云Azure的飞速发展和使用,每月我们都有近十万的新增订阅量.然而,我们也了解到还有很多企业在完全移到公有云 ...

  9. mongodb 3.4分片复制集配置

    1:启动三个实例 mongod -f /home/mongodb/db27017/mongodb27017.conf mongod -f /home/mongodb/db27018/mongodb27 ...

  10. POI 导出excel带小数点的数字格式显示不对解决方法

    最近看到了一个问题就是java导出excel中带小数点的数字显示不对, 比如我想在excel中第一行显示:  3,000.0 但是在excle中导出的格式总是不带小数点 3000(非文本格式),而且也 ...