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 - 1 or + 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.
#include"cstdio"
#include"queue"
#include"cstring"
using namespace std;
typedef pair<int,int> P;
const int MAXN=;
int vis[MAXN];
int n,k;
int bfs()
{
queue<P> que;
vis[n]=;
que.push(P(,n));
while(!que.empty())
{
P now = que.front();que.pop();
if(now.second==k)
{
return now.first;
}
for(int i=-;i<=;i++)
{
int next;
if(i==) next=now.second*;
else next=now.second+i;
if(next>=&&next<=MAXN&&!vis[next])//注意条件不能少,且顺序不能颠倒
{
vis[next]=;
que.push(P(now.first+,next));
}
}
}
return -;
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(vis,,sizeof(vis));
printf("%d\n",bfs());
}
return ;
}

virtual judge(专题一 简单搜索 C)的更多相关文章

  1. virtual judge(专题一 简单搜索 E)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  2. virtual judge(专题一 简单搜索 B)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  3. virtual judge(专题一 简单搜索 A)

    问题描述: Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘, ...

  4. [kuangbin带你飞]专题一 简单搜索 题解报告

    又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...

  5. [kuangbin带你飞]专题一 简单搜索(回顾)

    A - 棋盘问题 POJ - 1321 注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了. AC代码: #include<iostream> #include< ...

  6. kuangbin专题 专题一 简单搜索 Oil Deposits HDU - 1241

    题目链接:https://vjudge.net/problem/HDU-1241 题意:问有几个油田,一个油田由相邻的‘@’,组成. 思路:bfs,dfs都可以,只需要遍历地图,遇到‘@’,跑一遍搜索 ...

  7. kuangbin专题 专题一 简单搜索 迷宫问题 POJ - 3984

    题目链接:https://vjudge.net/problem/POJ-3984 这个题目,emm,上代码,看的估计应该是刚开始接触搜索的,我带点注释,你能慢慢理解. #include <ios ...

  8. kuangbin专题 专题一 简单搜索 Fire! UVA - 11624

    题目链接:https://vjudge.net/problem/UVA-11624 题意:一个迷宫,可能有一个或者多个地方着火了,每过1个时间消耗,火会向四周蔓延,问Joe能不能逃出迷宫,只要走出迷宫 ...

  9. kuangbin专题 专题一 简单搜索 Fliptile POJ - 3279

    题目链接:https://vjudge.net/problem/POJ-3279 题意:格子有两面,1表示黑色格子,0表示白色格子,奶牛每次可以踩一个格子,踩到的格子和它周围的上下左右格子都会翻面,也 ...

随机推荐

  1. why factory pattern and when to use factory pattern

    1 factory pattern本质上就是对对象创建进行抽象 抽象的好处是显然的,可以方便用户去获取对象. 2 使用factory pattern的时机 第一,当一个对象的创建依赖于其它很多对象的时 ...

  2. Netbeans8.0设置Consola字体并解决中文乱码问题

    在Netbeans8.0上开发php,设置字体为Consola后.发现中文显示是乱码的.经过改动jre的配置文件成功攻克了这个问题. 1. 进入jdk安装文件夹下/jre/lib文件夹,找到fontc ...

  3. VS2013 Pro版本密钥

    Visual Studio Professional 2013 KEY(密钥): XDM3T-W3T3V-MGJWK-8BFVD-GVPKY

  4. SQL优化小结

    一 背景      客户数据库经常出现死锁.超时.查询慢等问题,数据库mssql,数据量主要表大概上千W. 二 收集信息      首先是要找出IO大.查询慢.使用频率高的脚本.直接用Profiler ...

  5. 更改node版本

    npm install -g n n stable 或 n v4.5.0

  6. Ubuntu安装SSH + Windows上配置Putty

    1. Ubuntu安装SSH 命令: # sudo apt-get install openssh-server 2. 启动SSH Server 命令: # sudo /etc/init.d/ssh ...

  7. Python 3 面向对象 一

    Python 3 面向对象 一.面向过程-->面向对象 面向过程:根据业务逻辑从上到下堆叠代码,即先干什么再干什么,基于面向过程去设计程序就好比在设计一条流水线,是一种机械式的思维方式 函数式: ...

  8. 20145229吴姗珊 《Java程序设计》课程总结

    20145229吴姗珊 <Java程序设计>课程总结 (按顺序)每周读书笔记链接汇总 第一周:http://www.cnblogs.com/20145229ss/p/5248728.htm ...

  9. ios9 3dtouch 博客

    http://my.oschina.net/u/2340880/blog/511509#OSC_h3_3

  10. Nginx/Apache下如何禁止指定目录运行PHP脚本

    下面和大家一起分享下如何在Apache和Nginx禁止上传目录里PHP的执行权限. Apache下禁止指定目录运行PHP脚本 在虚拟主机配置文件中增加php_flag engine off指令即可,配 ...