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. JavaScript中url 传递参数(特殊字符)解决方法及转码解码的介绍

    有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.下表中列出了一些URL特殊符号及编码   十六进制值 1. + URL 中+号表示空格 %2B 2. 空 ...

  2. Dockerfile 模版

    最近一直在用docker,总结了一个靠谱的模版,分享给大家. From ubuntu:14.04 MAINTAINER pidong.li@genetronhealth.com RUN echo de ...

  3. Arcgis for javascript不同的状态下自定义鼠标样式

    俗话说:爱美之心,人皆有之.是的,没错,即使我只是一个做地图的,我也希望自己的地图看起来好看一点.在本文,给大家讲讲在Arcgis for javascript下如何自定义鼠标样式. 首先,说几个状态 ...

  4. The Pragmatic Programmer 摘要评注

    这本书与其说是一本编程书,倒不如说是一本教做人的书.很多时候项目的进行依赖于技术以外的因素,比如说沟通,人的品格,人际,处理问题的方法.在未来的一度日子会陆续添加个人认为值得学习的内容.

  5. c printf打印格式

    关于小数点位数的举例:  <pre lang="c" escaped="true">#include <stdio.h> /* 当fah ...

  6. C++中继承关系中的同名隐藏和对策

    在C++及其面向对象的理论中,有这样的场景:一个类继承自另外一个类,如果这两个类都有一个函数名和参数及其返回值一样的成员函数,那么子类的函数会自动将父类对应的函数隐藏.即同名隐藏.在有时的开发过程中, ...

  7. HDU3507Print Article (斜率优化DP)

    Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it ...

  8. 如何解决无法通过SSL加密与SQLServer建立连接

    在部署项目时,经常会遇到驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接,错误:Java.lang.RuntimeException: Could not gene ...

  9. SqlServer 临时表

    SqlServer中临时表分为两种:一种是局部(本地)临时表,用#TableName表示.一种是全局(服务器)临时表,用##TableName表示. 创建临时表: 1. create table #T ...

  10. LeetCode Maximum Swap

    原题链接在这里:https://leetcode.com/problems/maximum-swap/description/ 题目: Given a non-negative integer, yo ...