zoukankan      html  css  js  c++  java
  • CF520B——Two Buttons——————【广搜或找规律】

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

    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.

    出错点:没加标记数组,没有看数据限制范围。

    广搜:

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    struct node{
    
        int out;
        int dep;
    }pos;
    const int maxn=11000;
    bool mark[maxn];
    queue<node>Q;
    int aim;
    int bfs(int n){
    
        memset(mark,0,sizeof(mark));
        pos.out=n;
        pos.dep=0;
        Q.push(pos);
        mark[pos.out]=1;
        while(!Q.empty()){
    
            node tmp=Q.front();
            Q.pop();
            if(tmp.out!=aim){
    
               node tmp1,tmp2;
               tmp1.dep=tmp.dep+1;
               tmp1.out=tmp.out*2;
               if(tmp1.out>=1&&tmp1.out<=10000&&!mark[tmp1.out]){
                  
                    Q.push(tmp1);
                    mark[tmp1.out]=1;
               }
                tmp2.out=tmp.out-1;
                tmp2.dep=tmp.dep+1;
                if(tmp2.out>=1&&tmp2.out<=10000&&!mark[tmp2.out]){
                    
                    Q.push(tmp2);
                    mark[tmp2.out]=1;
                }
            }else{
    
                return tmp.dep;
            }
        }
    }
    int main(){
    
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
    
            while(!Q.empty()){
    
                Q.pop();
            }
    
            aim=m;
            int ans;
            if(n==aim){
    
                printf("0
    ");
            }else{
    
                ans=bfs(n);
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
    

    规律:可以逆向思考。n-1在某种意义上等同于m+1,n*2等同于m/2。

    #include<stdio.h>
    int main(){
    
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
    
            int cnt=0;
            if(n>=m){
    
                printf("%d
    ",n-m);
            }else{
    
                while(n!=m){
    
                    if(m&1){
    
                        m+=1;
                        cnt++;
                    }
                    m/=2;
                    cnt++;
                    if(n>m){
    
                        cnt+=n-m;
                        break;
                    }
                }
                printf("%d
    ",cnt);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    C# WinForm 关于窗体最大化时的是否全屏效果与是否遮盖任务栏
    C# winform 无边框 窗体的拖动
    lenovo 联想笔记本ideapad 320c-15改装win7问题
    解决WIN7第一次开机冷启动QQ未响应的办法
    WIN10X64_LTSB2016极限精简版by双心
    RAMOS和SSD对比
    联想IDEAPAD 320C-15笔记本显卡驱动问题
    WIN7以上系统安装VB6的解决办法,附上个批处理。
    吐槽下银联1分钱乘公交
    QQ聊天框变成方框口口口口的解决办法
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4335663.html
Copyright © 2011-2022 走看看