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
此题需要注意的是queue应该定义全局,这一点上我wa了好几发
 #include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int cur,step;
}st;
queue<node>q;
int m,n,ans,visit[];
int dfs(int x)
{
int i;
while(!q.empty())
{
q.pop();
}
memset(visit,,sizeof(visit));
node s,p;
q.push(st);
visit[st.cur]=;
while(!q.empty())
{
p=q.front();
q.pop();
if(p.cur==n)
return p.step;
for(i=;i<=;i++)
{
s=p;
if(i==)
s.cur-=;
else if(i==)
s.cur+=;
else
s.cur*=;
s.step++;
if(s.cur==n)
return s.step;
if(s.cur>=&&s.cur<=&&!visit[s.cur])
{
visit[s.cur]=;
q.push(s);
} }
} }
int main()
{
while(scanf("%d%d",&m,&n)!=-)
{
st.cur=m;
st.step=;
ans=dfs(m);
printf("%d\n",ans);
}
return ;
}

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. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

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

  4. Catch That Cow(BFS)

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

  5. ***参考Catch That Cow(BFS)

    Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  6. Catch That Cow (bfs)

    Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...

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

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

  8. 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)

    Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...

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

  10. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

随机推荐

  1. 【dlbook】机器学习基础

    [机器学习基础] 模型的 vc dimension 如何衡量? 如何根据网络结构衡量模型容量?有效容量和模型容量之间的关系? 统计学习理论中边界不用于深度学习之中,原因? 1.边界通常比较松, 2.深 ...

  2. React 源码剖析系列 - 生命周期的管理艺术

    目前,前端领域中 React 势头正盛,很少能够深入剖析内部实现机制和原理. 本系列文章 希望通过剖析 React 源码,理解其内部的实现原理,知其然更要知其所以然. 对于 React,其组件生命周期 ...

  3. 正则化项L1和L2的区别

    https://blog.csdn.net/jinping_shi/article/details/52433975 https://blog.csdn.net/zouxy09/article/det ...

  4. phpcms URL

    http://www.jb51.net/cms/112109.html 静态URL生成规则 http://v9.help.phpcms.cn/html/2010/database_1228/107.h ...

  5. Android spannableStringBuilder用法整理

    Android spannableStringBuilder用法整理 分类: Android开发2013-11-29 10:58 5009人阅读 评论(0) 收藏 举报 Androidspannabl ...

  6. upper_bound函数,binary_check函数

    个人心得:二分的经典运用,刚开始就是upper_bound可能难以实现一点,还有就是要注意没找到的时候 lower_bound 返回大于等于key的第一个元素的下标.upper_bound 返回大于k ...

  7. Doxygen—程序文档生成工具

    Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,完全支持C.C++.Java.Objective-C和IDL语言,部分支持PHP.C#.注释的语法与Qt-Doc.KDoc和J ...

  8. .NET 应用程序域?

    为了提升windows系统的稳定性与可靠性,windows通过进程来实现.所有的可执行代码.数据以及其他资源都被包含在进程中,不允许其他进程对它进行访问(除非有足够的权限).对于.NET应用程序,还进 ...

  9. Python 函数 set()

    set() 功能:       set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集.差集.并集等. iterable -- 可迭代对象对象:返回新的集合对象. 语法 ...

  10. sklearn one_hot 操作

    1.编码 one_hot编码不再过多叙述,类似于hash的那种方法去改变数的编码方式.比如label存在与(0,1,2,3),那么一条记录的label为3,那么将编码维[0,0,0,1] 2.包: t ...