zoukankan      html  css  js  c++  java
  • CodeForces 520B Two Buttons(用BFS)

     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 m out 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.

    题目很容易理解,从一个数,到另外一个数,在只有两步操作的情况下,用至少几步能到达。

    开始看人家标签,用dfs想些,搞了半天不会。。。然后想到用最少步,bfs应该可以。就试着写了。

     写了好几遍,人家数据范围是 一万, 我要开 十万 的数组才能过,这肯坑定是问题。果然, 在bfs里控制访问 now值二倍时,有问题,剪枝没剪对。

     1 ///当 n > m 时,并没有比 -1 更好的办法
     2 ///当 n < m 时, 就需要去寻找答案
     3 
     4 #include <cstdio>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <iostream>
     8 #include <queue>
     9 using namespace std;
    10 
    11 const int max_size = 10000 + 10;
    12 int step[max_size*2];
    13 int vis[max_size*2];
    14 
    15 int bfs(int n, int m)
    16 {
    17     int now;
    18     queue<int> que;
    19     que.push(n);
    20     vis[n] = 1;
    21     step[n] = 0;
    22 
    23     while(que.size())
    24     {
    25         now = que.front();
    26         if(now == m)
    27         {
    28             return step[now];
    29         }
    30         que.pop();
    31         if(now-1 > 1 && !vis[now-1])
    32         {
    33             que.push(now-1);
    34             vis[now-1] = 1;
    35             step[now-1] = step[now] + 1;
    36         }
    37         if(now <= m && !vis[now*2])
    38         {
    39             que.push(now*2);
    40             vis[now*2] = 1;
    41             step[now*2] = step[now] + 1;
    42         }
    43     }
    44 }
    45 
    46 int main()
    47 {
    48     int n, m;
    49     cin >> n >> m;
    50     memset(step, 0, sizeof(step));
    51     memset(vis, 0, sizeof(vis));
    52 
    53            if(n > m)
    54            cout << n - m << endl;
    55            else
    56     {
    57         cout << bfs(n, m) << endl;
    58     }
    59 return 0;
    60 }
    View Code
  • 相关阅读:
    (转贴)Visual Studio2005 + Visual SourceSafe 2005 实现团队开发、源代码管理、版本控制
    vss2003的资料说明,转贴自MSDN
    非常经典的网络蜘蛛示例,我是转载在这里的
    Vsi的路径所在
    (转)三种模拟自动登录和提交POST信息的实现方法
    (转)关于网络蜘蛛的知识
    (转)thin的制作DataGrid的HTC,转来自己用做开发
    转帖:麻雀虽小,五脏俱全-C# 创建windows服务、socket通讯实例
    Google Maps API编程资源大全
    C#实现的根据年月日计算星期几的函数(转)
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/4348699.html
Copyright © 2011-2022 走看看