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. Django之权限用法

    **记住每一个url都是一个权限** 注册 可插拔试的权限,可以先写其他的逻辑,在最后再把权限加上 将rbac组件拷贝到项目上,注册项目 修改表结构 将写好的用户表对rbac的User表进行一对一的关 ...

  2. Python过滤

    text = "A2A"s = filter(lambda ch: ch in '0123456789', text)print int(s)

  3. 感知器(Perception)

    感知器是一种早期的神经网络模型,由美国学者F.Rosenblatt于1957年提出.感知器中第一次引入了学习的概念,使人脑所具备的学习功能在基于符号处理的数学到了一定程度模拟,所以引起了广泛的关注. ...

  4. inline-block间距解决方案

    当我们将元素设为inline-block时,总是会莫名其妙出现一些间距 <!DOCTYPE html> <html> <head> <meta charset ...

  5. OSI模型网络分层

    OSI TCP/IP --- ------- 应用层 表示层 应用层 会话层 ----- ------- 传输层 TCP UDP ----- ------- 网络层 IPv4/IPv6 ----- - ...

  6. Windows 7 比Windows XP 难用的功能

    Windows 7 的搜索功能做得实在难用,不仅慢,还经常搜不到文件(明明存在的文件却搜不到).相比Windows XP,这个功能差的太远了. Windows 7 的无线连网功能,即使设置为不要自动连 ...

  7. 2.HelloWorld程序

    1.流程图 2./itcast0711/src/main/java/cn/itcast/a_helloworld/HelloWorld.java package cn.itcast.a_hellowo ...

  8. 表达式语句(EL)

    EL的基本语法 ${expression} Expression:制定要输出的变了或字符串.或EL运算符组成的表达式. 禁用EL表达式: 1. 使用“\”符号禁用. \${expression} 2. ...

  9. 记录quick cocos2d-x3.2升级至cocos2d-x3.8

    目前为止,quickcocos2d-x没有3.8版本,想用3.8又想用quick,所以只能自己升级了,自己先记录下,防止忘记. cocos2d-x3.8里面有quick framework,而simu ...

  10. php.ini中的session配置说明

    下面介绍能让session运行的必要配置步骤 手动配置PHP运行环境时,最容易遗忘的一项是服务器端session文件的存储目录配置工作,打开php.ini文件,搜索Session,找到session. ...