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

    /*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步。可以点红色按钮,蓝色按钮来改变数字,红色:*2,蓝色:-1,如果变成负数,
    就变成原来的数。
    CF 520 B. Two Buttons
    思路:当然是*2可以掠过的步数更少啦,如果n是输入,m是输出。

    如果n大于m,不能使用红色按钮,很容易看出,步数就是n-m。

    如果n比m小,如果m是偶数的话,m/2,如果m是奇数,m+1,这样一直循环判断n是不是还比m小,不符合就跳出循环,进入第一个如果。*/

    /*正向BFS
    #include<iostream>
    #include<queue>
    #include<string.h>
    #define ll long long
    using namespace std;
    ll n, m, cnt;
    ll vis[20000005], ans[20000005];
    queue<ll>a;
    int main()
    {
        while (cin >> n >> m)
        {
            memset(vis, 0, sizeof(vis));
            memset(ans, 0, sizeof(vis));
            while(!a.empty())
                a.pop();
            cnt = 0;
            if (n >= m)
                cout << n - m << endl;
            else
            {
                a.push(n);
                while (!a.empty())
                {
                    ll x = a.front();
                    a.pop();
                    if (x == m)
                    {
                        cnt = ans[x];
                        break;
                    }
    
                    if (x <= 2*m && x - 1 >= 0 && vis[x] == 0)
                    {
                        a.push(x - 1);
                        ans[x - 1] = ans[x] + 1;
                    }
                    if (x <= 2*m  && x != 0 && vis[x] == 0)
                    {
                        a.push(x * 2);
                        ans[x * 2] = ans[x] + 1;
                    }
                    vis[x] = 1;
                }
                cout << cnt << endl;
            }
            
    
        }
        return 0;
    }
    */
    
    
    //逆向规律模拟
     #include<iostream>
     #include<algorithm>
     #include<string.h>
     #include<math.h>
     #define ll long long
     using namespace std;
     ll a, b, ans;
     int main()
     {
         while (cin >> a >> b)
         {
             ans = 0;
             if (a >= b)
                 ans = a - b;
             else
             {
                while (a < b)
                 {
                     if (b % 2)
                     {
                         ans++;
                         b++;
                     }
                     else
                     {
                         ans++;
                         b /= 2;
                     }
                 }
                 ans += abs(a - b);
             }
             cout << ans << endl;
         }
         return 0;
     }
  • 相关阅读:
    ds
    使用js做的贪吃蛇游戏的知识总结
    使用js实现的关联表单
    使用jquery实现的计算器功能
    vue学习(2)关于过渡状态
    vue学习(1)生命周期
    vue学习(0)写在前面的一句话
    bootstrap(7)关于进度条,列表,面板
    bootstrap(6)分页,翻页,徽章效果
    异常捕获
  • 原文地址:https://www.cnblogs.com/-citywall123/p/9511743.html
Copyright © 2011-2022 走看看