zoukankan      html  css  js  c++  java
  • CF 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.

    刚开始T了,后来加入了剪枝,如果当前这个时间量搜过了那么就不再搜。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <string>
     5 #include <algorithm>
     6 #include <cctype>
     7 #include <queue>
     8 using    namespace    std;
     9 
    10 const    int    MAX = 10000;
    11 bool    VIS[MAX + 5];
    12 struct    Node
    13 {
    14     int    n;
    15     int    time;
    16 };
    17 
    18 
    19 void    bfs(int,int);
    20 int    main(void)
    21 {
    22     int    n,m;
    23 
    24     while(scanf("%d%d",&n,&m) != EOF)
    25     {
    26         if(n == m)
    27             puts("0");
    28         else    if(n > m)
    29             printf("%d
    ",n - m);
    30         else
    31             bfs(n,m);
    32     }
    33 }
    34 
    35 
    36 void    bfs(int n,int m)
    37 {
    38     fill(VIS,VIS + MAX,false);
    39     queue<Node>    que;
    40     Node    first;
    41     first.time = 0;
    42     first.n = n;
    43     que.push(first);
    44 
    45     while(!que.empty())
    46     {
    47         Node    cur = que.front();
    48         que.pop();
    49         for(int i = 0;i < 2;i ++)
    50         {
    51             Node    next = cur;
    52             if(!i)
    53             {
    54                 next.n *= 2;
    55                 next.time ++;
    56                 if(next.n > MAX)
    57                     continue;
    58             }
    59             else
    60             {
    61                 next.n --;
    62                 next.time ++;
    63                 if(next.n <= 0)
    64                     continue;
    65             }
    66             if(next.n == m)
    67             {
    68                 printf("%d
    ",next.time);
    69                 return    ;
    70             }
    71 
    72             if(VIS[next.n])
    73                 continue;
    74             VIS[next.n] = true;
    75             
    76             que.push(next);
    77         }
    78     }
    79 
    80     return    ;
    81 }
  • 相关阅读:
    【Vertica】vertica配置ODBC
    【oracle】【demo】oracle批量造数据
    【oracle】oracle系统视图nls_database_parameters
    【oracle】interval函数
    【DB2】DB2帮助文档
    【oracle】【sql】oracle去重
    【oracle】【sql】oracle分组
    【oracle】【性能】case表达式
    oracle EXP-00028
    Nginx系列~Nginx服务启动不了
  • 原文地址:https://www.cnblogs.com/xz816111/p/4399703.html
Copyright © 2011-2022 走看看