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. 从内存中加载并启动一个exe

    windows似乎只提供了一种启动进程的方法:即必须从一个可执行文件中加载并启动.而下面这段代码就是提供一种可以直接从内存中启动一个exe的变通办法.用途嘛, 也许可以用来保护你的exe,你可以对要保 ...

  2. Module 'curl' already loaded in Unknown on line 0

    Module 'curl' already loaded in Unknown on line 0 应该是php binary已经包含curl,你又动态加载了一遍.屏蔽掉你的extension 引用, ...

  3. 在VS2010下打开VS2008项目的解决办法

    如何在vs2010中打开vs2008项目文件? 第一步:以记事本方式打开该项目的sln解决方案,找到这两行信息,分别如下:Microsoft Visual Studio Solution File,  ...

  4. 第6条:在单次切片操作内,不要同时指定start、end和stride

    核心知识点: 1.使用负步进可以反转取值字符串及ASCII. 2.stride最好不要与start和end用在一起,会降低代码可读性. 除了基本的切片操作之外,python还提供了somelist[s ...

  5. Kindeditor 函数用途

    1.loadScript  加载文件   2.updateState 更新工具条状态   afterCreate在dom加载的时候执行,dom加载完之前执行的 K.ready dom加载完之后执行   ...

  6. 第二十三篇、IO多路复用 二

    基于IO多路复用实现socket的连接 下面流程:1.先创建列表,让自己的socket对象传入,然后遍历select第一个参数2.客户端连接,那么客户端socket就传入到了列表里面,3.遍历,接收客 ...

  7. curl使用手册

    查询curl耗时 curl -o /dev/null -s -w %{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_ ...

  8. Hadoop 2.x 之 HA 简介

    HA结构图 HA是用来解决单点故障问题 DN: DataNode,启动时会往所有的NameNode汇报 NN: NameNode(主 Active(一个)   备 Standby(可以有多个)) Jo ...

  9. BioNLP概述

    BioNLP概述 工具: GENIA Tagger:GENIA Tagger是一个主要应用于生物医学文本领域的词性标注和浅层语法分析工具,GENIA Tagger在GENIA语料上的词性标记性能F-s ...

  10. M1905

    11.09    11:00------102万 11.09     14:00---103万 11.12    16:00------103万 11.19     16:00---94万 11.20 ...