bfs实现问题---数学

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

 Status

Description

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 Input

Input
4 6
Output
2
Input
10 1
Output
9

Hint

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.

题意:有一种装置,有两种运算方式,在原来的基础上 乘2或者是-1. 给你两个数字问将第一个数字转化为第二个数字需要几次。 分析:当做一个隐式图来枚举。bfs与spfa都可以。  

下面用(BFS)来实现

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int Max=2e4+4;
int vis[Max+5];
typedef struct {
    int x,step;
}node;

void bfs(int n,int m)
{
    memset(vis,0,sizeof(vis));
    queue<node> q;
    vis[n]=1;
    node t;
    t.x=n;  t.step=0;
    q.push(t);
    while(!q.empty()){
        t=q.front();    q.pop();
        node t1,t2;
        t1=t2=t;
        t1.x*=2;    t1.step++;
        t2.x-=1;    t2.step++;
//        printf("%d %d --  %d %d **********\n",t1.x,t1.step,t2.x,t2.step);
        if(t1.x==m){
            printf("%d\n",t1.step);
            break;
        }
        if(t2.x==m){
            printf("%d\n",t2.step);
            break;
        }
        if(t1.x > 0 && t1.x < Max && !vis[t1.x]){
            q.push(t1);
            vis[t1.x]=1;
        }
        if(t2.x > 0 && t2.x < Max && !vis[t2.x]){
            q.push(t2);
            vis[t2.x]=1;
        }
    }

}
int main()
{
    int n,ans;
    while(~scanf("%d%d",&n,&ans)){
        if(ans < n){
            printf("%d\n",n-ans);
            continue;
        }
        else if(ans == n){
            printf("0\n");
            continue;
        }
        else{
            bfs(n,ans);
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黎轩栀海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值