Two Buttons
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.

Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?

Input

The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .

Output

Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.

Sample test(s)
Input
4 6
Output
2
Input
10 1
Output
9
Note

In the first example you need to push the blue button once, and then push the red button once.

In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.

题目很容易理解,从一个数,到另外一个数,在只有两步操作的情况下,用至少几步能到达。

开始看人家标签,用dfs想些,搞了半天不会。。。然后想到用最少步,bfs应该可以。就试着写了。

写了好几遍,人家数据范围是 一万, 我要开 十万 的数组才能过,这肯坑定是问题。果然, 在bfs里控制访问 now值二倍时,有问题,剪枝没剪对。

 ///当 n > m 时,并没有比 -1 更好的办法
///当 n < m 时, 就需要去寻找答案 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std; const int max_size = + ;
int step[max_size*];
int vis[max_size*]; int bfs(int n, int m)
{
int now;
queue<int> que;
que.push(n);
vis[n] = ;
step[n] = ; while(que.size())
{
now = que.front();
if(now == m)
{
return step[now];
}
que.pop();
if(now- > && !vis[now-])
{
que.push(now-);
vis[now-] = ;
step[now-] = step[now] + ;
}
if(now <= m && !vis[now*])
{
que.push(now*);
vis[now*] = ;
step[now*] = step[now] + ;
}
}
} int main()
{
int n, m;
cin >> n >> m;
memset(step, , sizeof(step));
memset(vis, , sizeof(vis)); if(n > m)
cout << n - m << endl;
else
{
cout << bfs(n, m) << endl;
}
return ;
}

CodeForces 520B Two Buttons(用BFS)的更多相关文章

  1. CodeForces 520B Two Buttons

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Vasya ...

  2. Codeforces.520B.Two Buttons(正难则反)

    题目链接 \(Description\) 给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2.求最少需要多少次可以使\(n\)等于\(m\). \(Solution\) 暴力连边BF ...

  3. Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )

    B. Two Buttons time limit per test : 2 seconds memory limit per test :256 megabytes input :standard ...

  4. 【codeforces 520B】Two Buttons

    [题目链接]:http://codeforces.com/contest/520/problem/B [题意] 给你一个数n; 对它进行乘2操作,或者是-1操作; 然后问你到达m需要的步骤数; [题解 ...

  5. Codeforces 520B:Two Buttons(思维,好题)

    题目链接:http://codeforces.com/problemset/problem/520/B 题意 给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m 思路 ...

  6. cf520B-Two Buttons 【BFS】

    http://codeforces.com/contest/520/problem/B Two Buttons Vasya has found a strange device. On the fro ...

  7. codeforces 520 Two Buttons

    http://codeforces.com/problemset/problem/520/B B. Two Buttons time limit per test 2 seconds memory l ...

  8. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  9. CodeForces 540C Ice Cave (BFS)

    http://codeforces.com/problemset/problem/540/C       Ice Cave Time Limit:2000MS     Memory Limit:262 ...

随机推荐

  1. MVC前后端数据被编码

    @{ ViewBag.Title = "Home Page";}<script> function htmldecode(s) { console.log(s); va ...

  2. Python Day5

    模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个 ...

  3. Effective java读书笔记

    2015年进步很小,看的书也不是很多,感觉自己都要废了,2016是沉淀的一年,在这一年中要不断学习.看书,努力提升自己 计在16年要看12本书,主要涉及java基础.Spring研究.java并发.J ...

  4. QT中检索设定目录下所有指定文件的方法

    void MainWindow::on_pushButton_clicked() { QDir dir=QFileDialog::getExistingDirectory(this, tr(" ...

  5. hzwer模拟赛 感冒病毒

    题目描述 Description 一种感冒病毒正在学校里传播,这所学校有n个学生,m个学生社团,每个学生可能参加了多个社团,因为同一个社团的学生交流较多,所以如果一个学生感染上感冒病毒,那么他所在的社 ...

  6. eclipse android工程没有错却出现红叉

    [转]eclipse android工程没有错却出现红叉 问题描述: 这是一个很变态的问题,花了我N多时间才解决掉,而且弄得心情非常郁闷,这明显是ADT的bug嘛,为什么最新的版本还没有解决? 将Li ...

  7. Mysql存中文值乱码

    一是安装mysql时,其中会有一个步骤选择编码方式,此时选择gbk即可.如果不选择,默认的编码是latin1: 二是在安装玩mysql之后,手动修改其配置文件,如下: (1)修改 MySql安装目录下 ...

  8. Windows下memcached.exe的安装与配置

    D:\PHP\Memcached\memcached.exe -d install D:\PHP\Memcached\memcached.exe –m  1024  -d start 假设安装在:D: ...

  9. css-单位%号-background-size-background-position-遁地龙卷风

    (-1)写在前面 我用的是chrome49,这篇是为后续做准备.重要性的调整以及毕业资料的整体导致最近没看JQuery和H5特效,以后只能晚上看了. (0)准备 div长宽都为300px,我们一张大小 ...

  10. html嵌入样式表

    1.针对文件中的字体还有属性进行设置主要设置文字的大小及其颜色问题,未涉及div飘操作 处理页面CSS 先检测该内容部分是否已经设定了样式,如果没有单独设定再按照总体设计进行限定. eg:  h1 h ...