zoukankan      html  css  js  c++  java
  • codeforces 520 B. Two Buttons (bfs)

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

    做过一道类似的题

    因为是问最短,很容易想到是bfs

    对于点x,可以到达点x-1,和点2*x

    需要注意的是上界限。

    并不是max(m,n)

    因为可能先达到比m大,之后再减回来的情况是更优的。

    max(2*m,2*n)肯定是足够的

    /*************************************************************************
        > File Name: code/cf/#295/B.cpp
        > Author: 111qqz
        > Email: rkz2013@126.com 
        > Created Time: 2015年08月17日 星期一 04时16分51秒
     ************************************************************************/
    
    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<queue>
    #include<vector>
    #include<stack>
    #define y0 abc111qqz
    #define y1 hust111qqz
    #define yn hez111qqz
    #define j1 cute111qqz
    #define tm crazy111qqz
    #define lr dying111qqz
    using namespace std;
    #define REP(i, n) for (int i=0;i<int(n);++i)  
    typedef long long LL;
    typedef unsigned long long ULL;
    const int inf = 0x7fffffff;
    const int N=1E6+7;
    int n,m;
    int d[N];
    int mx;
    
    void bfs()
    {
        memset(d,-1,sizeof(d));
        d[n] = 0 ;
        queue<int>q;
        q.push(n);
        while (!q.empty())
        {
        int px = q.front();q.pop();
        if (px==m)
        {
            cout<<d[m]<<endl;
            return;
        }
    
        int nxt[2];
        nxt[0] = px - 1;
        nxt[1] = 2*px;
        for ( int i = 0 ; i < 2 ; i++)
        {
            if (nxt[i]>=1&&nxt[i]<=mx&&d[nxt[i]]==-1)
            {
            d[nxt[i]] = d[px] + 1;
            q.push(nxt[i]);
            }
        }
        }
    }
    int main()
    {
        scanf("%d %d",&n,&m);
        mx = max(2*n,2*m);
        bfs();
    
      
    return 0;
    }
  • 相关阅读:
    mybatis-generator.xml
    模板下载
    任务导出
    订单导出
    创建指定日期任务
    字符串的知识
    数组元素反转
    JVM 中知识
    Redis介绍
    JsonP
  • 原文地址:https://www.cnblogs.com/111qqz/p/4735464.html
Copyright © 2011-2022 走看看