B. 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.

题目和算法分析:

在每组测试数据里,输入n和m的值,求从n变到m最少所需要的变换次数。每次对于n的操作可以是:*2 或者 -1。

比如第一组测试数据:要从n=4变到m=6,需要将n=4-1=3, 再3*2=6=m,这样最小且只需要2步。

通过bfs进行广度优先搜索,注意已经搜索过的数值不进行第二搜索,所以需要标记数组。此外还需要一个记录步数的数组。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#define N 10000+1000
#define M 10000 using namespace std; int n, m;
bool vis[N];
int dis[N]; void bfs()
{
queue<int>q;
q.push(n);
vis[n]=true;
dis[n]=;
int cur;
while(!q.empty())
{
cur=q.front(); q.pop();
if(cur==m)
{
printf("%d\n", dis[cur]);
return ;
}
else
{
if(cur->= && cur<=M && vis[cur-]==false)
{
vis[cur-]=true;
q.push(cur-);
dis[cur-]=dis[cur]+;
}
if(cur*>= && cur*<=M && vis[cur*]==false)
{
vis[cur*]=true;
q.push(cur*);
dis[cur*]=dis[cur]+;
}
}
}
} int main()
{
scanf("%d %d", &n, &m);
memset(dis, , sizeof(dis));
memset(vis, false, sizeof(vis));
bfs();
return ;
}

Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )的更多相关文章

  1. 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 inpu ...

  2. Codeforces Round #295 (Div. 2) B. Two Buttons

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

  3. Codeforces Round #295 (Div. 2) B. Two Buttons 520B

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

  4. 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons

    题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...

  5. Codeforces Round #295 (Div. 2) B. Two Buttons (DP)

    题意:有两个正整数\(n\)和\(m\),每次操作可以使\(n*=2\)或者\(n-=1\),问最少操作多少次使得\(n=m\). 题解:首先,若\(n\ge m\),直接输出\(n-m\),若\(2 ...

  6. Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索

    Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx  ...

  7. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  8. Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)

    Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...

  9. Codeforces Round #295 (Div. 2)

    水 A. Pangram /* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> # ...

随机推荐

  1. Codeforces 877E Danil and a Part-time Job(dfs序 + 线段树)

    题目链接   Danil and a Part-time Job 题意    给出一系列询问或者修改操作 $pow$ $x$表示把以$x$为根的子树的所有结点的状态取反($0$变$1$,$1$变$0$ ...

  2. android添加桌面悬浮窗

    1. 添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 2. ...

  3. AGC006

    AtCoder Grand Contest 006 <br > 心血来潮,开了一套AGC..... 然后发现各种不会做.........感觉智商被AGC摁在地上摩擦...... <b ...

  4. mac git安装及github配置

    准备下载一个react的demo程序包,需要本地用到git.早就向配置了,那就安装配置一下吧. 首先,原来mac已经安装了git,版本 2.7 ,我用 brew又安装了一份git 版本 2.10.2. ...

  5. 三维场景如何嵌入到PPT中展示?

    今天要跟大家一起交流的大体内容如标题所示,日常生活中,ppt已经成为人们工作学习生活中不可或缺的工具之一,那么三维场景是如何在ppt中加载展示的呢?请大家慢慢往下看. 1.创建命令按钮和web bro ...

  6. AngularJS的Foreach循环示例

    代码下载:https://files.cnblogs.com/files/xiandedanteng/angularJSForeach.rar 代码: <!DOCTYPE HTML PUBLIC ...

  7. Oracle数据库有用函数

    有用函数 DECODE 语法例如以下: DECODE(value, if1, then1, if2,then2,if3,then3, . . . else )  Value 代表某个表的不论什么类型的 ...

  8. hibernate的注解装配

    1.多对多,(中间表不用映射) @ManyToMany @JoinTable(name = "中间表名", joinColumns = { @JoinColumn(name = & ...

  9. python 调用函数时使用星号 *, **

    python 调用函数时使用星号 *, ** 调用函数时使用星号 * 或 ** test(*args):* 的作用其实就是把序列 args 中的每个元素,当作位置参数传进去.如果 args 等于 (1 ...

  10. SDUT 1068-Number Steps(数学:直线)

    Number Steps Time Limit: 1000ms   Memory limit: 10000K  有疑问?点这里^_^ 题目描写叙述 Starting from point (0,0) ...