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. SPOJ 7001 VLATTICE【莫比乌斯反演】

    题目链接: http://www.spoj.com/problems/VLATTICE/ 题意: 1≤x,y,z≤n,问有多少对(x,y,z)使得gcd(x,y,z)=1 分析: 欧拉搞不了了,我们用 ...

  2. const T、const T*、T *const、const T&、const T*& 的区别

    原文地址: http://blog.csdn.net/luoweifu/article/details/45600415 这里的T指的是一种数据类型,可以是int.long.doule等基本数据类型, ...

  3. System.Length 函数

    function _PCharLen(P: _PAnsiChr): Longint;{$IFNDEF LEGACY_PCHARLEN}begin  Result := 0;  if P <> ...

  4. Interface Builder中的技巧

    在我工作中经常会遇到有人吐槽Xcode中的interface builder(以下简称IB)不好用的开发者.在我看来,IB是一个非常棒的可视化开发工具,可以非常快捷的设置UI控件的大部分常用属性.下面 ...

  5. NSThread学习

    使用多线程可以防止主线程阻塞.同时也可以将一个大的任务分成若干个小的任务去做. 常用方法一: 1, 首先使用  detachNewThreadSelector:toTarget:withObject: ...

  6. MySQL的1067错误

    1.打开my.ini文件,找到default-storage-engine=InnoDB这一行,把它改成default-storage-engine=MyISAM.*** my.ini必须为ansi格 ...

  7. 我们为什么要把Dagger2,MVP以及Rxjava引入项目中?

    1Why? 我们为什么要把Dagger2,MVP以及Rxjava引入项目中? 毫无疑问在Android开发圈中这三个技术是经常被提及的,如此多的文章和开源项目在介绍他们,使用他们,开发者也或多或少的被 ...

  8. VC 读取服务器上的文件(HTTP方式) [转]

    CString GetStringFromUrl(LPCTSTR pszUrl){    CString str ;    HINTERNET hSession = ::InternetOpen( _ ...

  9. Exchange 2013 Database Move to New Partition

    建议不要删除默认数据库,可以通过修改默认数据库名称.路径等实现您的需求. 客戶:The HK Anti-Cancer Society. 要求:遷移數據庫(01)到新分區,實際是遷移成為數據庫(05)  ...

  10. Solaris分布式文件系统NFS

    NFS存在的意思是让不同unix系统之间可以传输数据.这样可以合理的利用资源.   对每一个系统,建立一个NFS文件系统,进行数据的备份. NFS守护进程: nfsd mountd locked st ...