zoukankan      html  css  js  c++  java
  • 搜索

    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.

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

    题目分析 : 有两种操作可以执行,一是将所给的树扩大2倍,2是将所给的数字减 1 , 之前单纯的写了一个过搜,run time 了....... 后来一位 大佬告诉了我,这个题得剪枝,不然你所开的数组就是会越界。

    代码示例 :

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <set>
    #include <time.h>
    using namespace std;
    const int eps = 1e5+5;
    const int maxn = 0x3f3f3f3f;
    #define ll long long
    int n, m;
    ll bu[eps];
    queue<int>que;
    
    void bfs() { 
        que.push(n);
        
        bu[n] = 0;
        while(!que.empty()) {
            int p = que.front();
            que.pop();
            if (p == m) break;
            int x1 = p*2;
            int x2 = p-1;
            if (bu[x1] == -1 && x1 < 20000) {
                que.push(x1);
                bu[x1] = bu[p] + 1;
            }
            if (bu[x2] == -1 && x2 < 20000) {
                que.push(x2);
                bu[x2] = bu[p] + 1;
            }
        }
    }
    
    int main() {
        
        scanf("%d%d", &n, &m);
        memset(bu, -1, sizeof(bu));
        //memset(vis, false, sizeof(vis));
        bfs();
        printf("%lld
    ", bu[m]);
        
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    带宽利用率提升50%,腾讯云联网架构方案解析
    重构实践:基于腾讯云Elasticsearch搭建QQ邮箱全文检索
    存算分离下写性能提升10倍以上,EMR Spark引擎是如何做到的?
    秒级去重:ClickHouse在腾讯海量游戏营销活动分析中的应用
    降本提效,贝壳搜索推荐架构统一之路
    亿级用户,腾讯看点信息流推荐系统的架构挑战
    优秀程序员,如何提高架构能力?
    如何创建体元栅格?
    导出属性表字段结构
    去除镶嵌数据集中影像的黑边或白边
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8308689.html
Copyright © 2011-2022 走看看