题目链接

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

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

分析:

每次走的话可以有三种走法,-1,+1,*2

代码:

#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
#include<math.h>
#include<queue>
using namespace std;
int N=1000000;
int a[1010000]={0};防止走重,即第一步走过了后面不应该再走一次
struct ft//结构体,包含数和步数
{
int x,y;
}p;
int k;
int bfs(int n)
{
queue<ft>Q;//建队
p.x=n,p.y=0;
a[n]=1;
Q.push(p);入队
ft cur, nex;
while(!Q.empty())
{
cur=Q.front();取队首元素
Q.pop();出队
if(cur.x==k)return cur.y;//判断是否达到目的
nex.x=cur.x+1;//判断+1的操作是否合法
if(nex.x>=0&&nex.x<N&&a[nex.x]==0)
{
nex.y=cur.y+1;
a[nex.x]=1;
Q.push(nex);合法则入队并步数+1
}
nex.x=cur.x-1;
if(nex.x>=0&&nex.x<N&&a[nex.x]==0)
{
nex.y=cur.y+1;
a[nex.x]=1;
Q.push(nex);
}
nex.x=cur.x*2;
if(nex.x>=0&&nex.x<N&&a[nex.x]==0)
{
nex.y=cur.y+1;
a[nex.x]=1;
Q.push(nex);
}
}
return -1;
}
int main()
{
int n;
while(~scanf("%d %d",&n,&k))
{
memset(a,0,sizeof(a));//多次运行,刷0;
int bs=bfs(n);
if(bs>=0)
printf("%d\n",bs);
}
return 0;
}

HDU 2717 Catch That Cow (深搜)的更多相关文章

  1. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

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

  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/Ot ...

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

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

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

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

  7. 杭电 HDU 2717 Catch That Cow

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

  8. HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............

    看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...

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

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

随机推荐

  1. lintcode-176-图中两个点之间的路线

    176-图中两个点之间的路线 给出一张有向图,设计一个算法判断两个点 s 与 t 之间是否存在路线. 样例 如下图: for s = B and t = E, return true for s = ...

  2. iOS-cocoapods使用方法

    1.CocoaPods的安装及使用: http://code4app.com/article/cocoapods-install-usage http://objccn.io/issue-6-4/ h ...

  3. 【bzoj3772】精神污染 STL+LCA+主席树

    题目描述 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区,是日本西部门户 ...

  4. IBatis Map时间参数文字格式不匹配!

    CS. ht.Add("start_time", startTime); Map <isNotNull prepend="and" property=&q ...

  5. Signal函数

    Signal函数: 这个函数是一种系统调用,就是告诉系统发生中断的时候用该干嘛.第一个参数就是信号的编号,第二个参数就是信号的指针. 原型: #include <signal.h> voi ...

  6. [POI2014]FAR-FarmCraft 树形DP + 贪心思想

    (感觉洛谷上题面那一小段中文根本看不懂啊,好多条件都没讲,直接就是安装也要一个时间啊,,,明明不止啊!还好有百度翻译......) 题意:一棵树,一开始在1号节点(root),边权都为1,每个点有点权 ...

  7. Android Bitmap和Drawable互转及使用BitmapFactory解析图片流

    一.Bitmap转Drawable Bitmap bmp=xxx; BitmapDrawable bd=new BitmapDrawable(bmp); 因为BtimapDrawable是Drawab ...

  8. POJ2135:Farm Tour——题解

    http://poj.org/problem?id=2135 题目大意: 从1到n再回来,每条边只能走一次,问最短路. —————————————————— 如果不告诉我是费用流打死不会想这个…… 我 ...

  9. BZOJ1833:[ZJOI2010]数字计数——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1833 https://www.luogu.org/problemnew/show/P2602 给定两 ...

  10. 注册google账号时出现手机号的问题

    注册谷歌账号时出现此电话号码无法用于进行验证 这主要是86和手机号之间有一个空格造成的,把这个空格删除就可以了