Two Buttons(BFS)

本博客探讨了一个关于设备按钮操作的问题,目标是在给定初始数字n和目标数字m的情况下,通过红蓝按钮的操作,求得到达目标数字所需的最小操作次数。详细介绍了输入输出格式、样例解析及AC代码。


Link:http://codeforces.com/problemset/problem/520/B



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.



AC  code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define LL long long
#define MAXN  100010
using namespace std;
int vis[10010],n;
struct node{
  LL x;
  LL sp;
}st,ed;
LL bfs()
{
   memset(vis,0,sizeof(vis));
   queue<node>Q;
   node now,next;
   int i;
   st.sp=0;
   Q.push(st);
   vis[st.x]=1;
   while(!Q.empty())
   {
      now=Q.front();
      if(now.x==ed.x)
         return now.sp;
      Q.pop();
      for(i=1;i<=2;i++)
      {
        switch(i)
       {
          case 1:
         {
            next.x=now.x*2;
            if(next.x>0&&next.x<10001&&!vis[next.x])
            {
                next.sp=now.sp+1;
                if(next.x==ed.x)
                    return next.sp;
                Q.push(next);
                vis[next.x]=1;
            }
            break;
        }
        case 2:
        {
            next.x=now.x-1;
            if(next.x>0&&next.x<10001&&!vis[next.x])
            {
                next.sp=now.sp+1;
                if(next.x==ed.x)
                return next.sp;
                Q.push(next);
                vis[next.x]=1;
            }
        break;}
}
}
}
return -1;
}
int main()
{
    LL n,m;
    while(cin>>n>>m)
    {
        st.x=n;
        ed.x=m;
        cout<<bfs()<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值