传送门: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. CSS之after与before的content 和 attr 配合使用

    content 和 attr 配合使用 如果你不想把content内容在CSS里写死,那你可以使用attr表达式来从页面元素中动态的获取内容: /* <div data-line="1 ...

  2. 00HTML

    一.概述 超文本标记语言(Hyper Text Markup Language),HTML是一门描述性的语言.基本语法: <标签> 内容 </标签>** 在一个网页中,HTML ...

  3. Hello Activemq

    0. 如果永远是localhost 可能一直low下去 1.下载安装 activemq 1.1 从官网下载activemq.tar.gz 并上传(rz)到linux系统 并解压 tar zxvf /* ...

  4. 前端学习之路之CSS (一)

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 简介:    CSS 指层叠样式表 (Cascading Style Sheets)    样式定义如何显示 HT ...

  5. 软件项目技术点(7)——在canvas上绘制自定义图形

    AxeSlide软件项目梳理   canvas绘图系列知识点整理 图形种类 目前我们软件可以绘制出来的形状有如下这几种,作为开发者我们一直想支持用户可以拖拽的类似word里面图形库,但目前还没有找到比 ...

  6. 【Udacity】机器学习性能评估指标

    评估指标 Evaluation metrics 机器学习性能评估指标 选择合适的指标 分类与回归的不同性能指标 分类的指标(准确率.精确率.召回率和 F 分数) 回归的指标(平均绝对误差和均方误差) ...

  7. Javascript之DOM的三大节点及部分用法

    DOM有三种节点:元素节点.属性节点.文本节点. 一.用nodeType可以检测节点的类型 节点类型 nodeType属性值 元素节点 1 属性节点 2 文本节点 3 这样方便在js中对各个节点进行操 ...

  8. 面试准备5之rest-framework部分

    rest-framework部分 1.你理解的Http协议? 答:1超文本协议,基于tcp协议的应用层协议,端口号80 本质就是一个socket客户端.请求-->响应-->断开 2 无连接 ...

  9. ubuntu18.04安装谷歌浏览器

    sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/sources.list.d/ wget -q ...

  10. BASE64编码的字符进行URL传输丢失特殊字符的问题

    因为BASE64的编码里含有“+”号等特殊字符,在url传输的时候会把+号编程空格,解决这个问题的方法: 请求时把BASE64编码进行url的编码再进行传输 接收时把BASE64编码进行url的解码 ...