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. 区分Web前端和后端(转载)

    转载自:http://blog.csdn.net/rosetta/article/details/53871766 前言   做C开发将近六年,基本上没有接触过web相关的东西,原来听别人说web相关 ...

  2. Django 之restframework1

    Restframework 这里先简单的介绍一下restful协议 ----一切皆是资源,操作只是请求方式 基于restful协议的框架有很多Django下的restframework只是其中的一种 ...

  3. go语言之并发编程 channel(1)

    单向channel: 单向通道可分为发送通道和接收通道.但是无论哪一种单向通道,都不应该出现在变量的声明中,假如初始化了这样一个变量 var uselessChan chan <- int =m ...

  4. MySQL5.7.26 忘记Root密码小计

    以前直接修改mysql.user就ok了,现在不行了,正好虚拟机MySQL的root密码忘记了,就简单记录下:(本方法不适合互联网线上项目,除非你不在意这段时间的损失) PS:以UbuntuServe ...

  5. ABAP--关于字符串String到XString XString to String转换代码

    转自http://guanhuaing.iteye.com/blog/1498891 代码如下 report zrich_0001. data: s type string, h(1) type x, ...

  6. (转)Javascript模块化编程(三):require.js的用法

    这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的 ...

  7. HTML5离线存储和本地缓存

    一.离线存储 有一个web应用有三个文件index.html,a.js,b.css,现在需要把js和css文件缓存起来 1.在index.html里加上<html manifest=" ...

  8. C#转换人民币大写金额

    /// <summary> /// 转换人民币大写金额. /// </summary> public class RMBConverter { /// <summary& ...

  9. 【Flask】模板继承

    # 模版继承笔记: ### 为什么需要模版继承:模版继承可以把一些公用的代码单独抽取出来放到一个父模板中.以后子模板直接继承就可以使用了.这样可以重复性的代码,并且以后修改起来也比较方便. ### 模 ...

  10. HDFS常见问题

    在HDFS里面,data node上的块大小默认是64MB(或者是128MB或256MB) 问题: 为什么64MB(或128MB或256MB)是最优选择? 为什么不能远少于64MB(或128MB或25 ...