Two Buttons(BFS)

本博客探讨了一个关于设备按钮操作的问题,目标是在给定初始数字n和目标数字m的情况下,通过红蓝按钮的操作,求得到达目标数字所需的最小操作次数。详细介绍了输入输出格式、样例解析及AC代码。


Link:http://codeforces.com/problemset/problem/520/B



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



AC  code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define LL long long
#define MAXN  100010
using namespace std;
int vis[10010],n;
struct node{
  LL x;
  LL sp;
}st,ed;
LL bfs()
{
   memset(vis,0,sizeof(vis));
   queue<node>Q;
   node now,next;
   int i;
   st.sp=0;
   Q.push(st);
   vis[st.x]=1;
   while(!Q.empty())
   {
      now=Q.front();
      if(now.x==ed.x)
         return now.sp;
      Q.pop();
      for(i=1;i<=2;i++)
      {
        switch(i)
       {
          case 1:
         {
            next.x=now.x*2;
            if(next.x>0&&next.x<10001&&!vis[next.x])
            {
                next.sp=now.sp+1;
                if(next.x==ed.x)
                    return next.sp;
                Q.push(next);
                vis[next.x]=1;
            }
            break;
        }
        case 2:
        {
            next.x=now.x-1;
            if(next.x>0&&next.x<10001&&!vis[next.x])
            {
                next.sp=now.sp+1;
                if(next.x==ed.x)
                return next.sp;
                Q.push(next);
                vis[next.x]=1;
            }
        break;}
}
}
}
return -1;
}
int main()
{
    LL n,m;
    while(cin>>n>>m)
    {
        st.x=n;
        ed.x=m;
        cout<<bfs()<<endl;
    }
    return 0;
}



A player wants to press the direction buttons as little as possible so that both frogs arrive at the destination to finish the game. Find the minimum number of times the player pressed the buttons to finish the game. If the frog or green frog cannot arrive at the destination, output -1. [Figure] For example, let's look at the case where a grid with six rows and six columns is given. The frog is located at (6, 1), the green frog is located at (1, 6), and the destination is (2, 3) as shown in the [Figure]. Walls are marked with #. First, the frog arrives at the destination first by moving eight times as shown on the left side of the [Figure], and the green frog moves to the opposite direction at the same time and is located at (6, 4). Next, as shown on the right side of the [Figure], when the green frog arrives at the destination by moving it five times, you pressed the buttons 13 times for both frogs to arrive at the destination. This is the minimum number of times you press the buttons. [Constraints] 1. N and M are integers from 2 to 40. 2. R1, R2, and R3 are integers from 1 to N. 3. C1, C2, and R3 are integers from 1 to M. 4. Both frogs cannot be at the same location while they are moving. 5. Walls are not the start and destination of both frogs, and the start positions of the two frogs are not their destinations. [Input] First, the number T of test cases is given, followed by T test cases. On the first line of each test case, N and M are given, separated by spaces. On the next line, R1, C1, R2, C2, R3, and C3 are given in the order, separated by spaces. Over the next N lines, a string of length M is given..” represents a position in the grid to which both frogs can move, and “#” represents a wall. [Output] Output one line per test case. For each test case, output “#x” (where x is the number of the test case, starting from 1), leave a space, and then output the answer of the relevant test case. [Example of Input and Output] (Input) 1 6 6 6 1 1 6 2 3 ....#. .#..#. .#..#. .#..#. .#.##. .#....
最新发布
09-16
### Two Buttons UI Design or Implementation in Frontend Development In the context of frontend development, designing and implementing two buttons involves understanding both the principles of **atomic design** as well as leveraging appropriate frameworks like Aura for structuring components effectively. Atomic design categorizes user interface (UI) elements into five distinct levels: atoms, molecules, organisms, templates, and pages[^1]. This methodology ensures that even simple designs such as button pairs are treated systematically. For instance, when creating a pair of buttons within an application using modern practices: #### HTML Structure Example The following code demonstrates how to structure two buttons semantically. ```html <div class="button-group"> <button id="primary-button">Primary Action</button> <button id="secondary-button">Secondary Action</button> </div> ``` This approach aligns closely with atomic design where individual `button` tags represent basic building blocks ("atoms"), while their combination inside `.button-group` forms more complex structures known as "molecules"[^1]. #### Styling Using CSS To enhance visual appeal without compromising accessibility standards: ```css .button-group { display: flex; gap: 1rem; /* Space between buttons */ } #primary-button { background-color: #007bff; color: white; border: none; padding: .5em 1em; cursor: pointer; } #secondary-button { background-color: lightgray; color: black; border: none; padding: .5em 1em; cursor: pointer; } ``` Such styling adheres not only to aesthetic guidelines but also integrates smoothly under larger systems built upon modular architectures similar to what might be found in framework-based solutions mentioned earlier [^2]. Additionally, ensuring proper interaction behaviors through JavaScript can further elevate usability aspects associated specifically around dual-action scenarios involving these types of controls.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值