博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #295 (Div. 2)B - Two Buttons BFS
阅读量:5375 次
发布时间:2019-06-15

本文共 1969 字,大约阅读时间需要 6 分钟。

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.

两种操作:

1.乘以2

2.减一

然后直接跑一发bfs就好啦

struct node{    int x;    int step;};int vis[maxn];int n,m;int main(){    cin>>n>>m;    queue
q; q.push({n,0}); vis[n]=1; while(!q.empty()) { node now=q.front(); q.pop(); if(now.x==m) { cout<
<
maxn-1||next.x<0) continue; vis[next.x]=1; q.push(next); } else { next.x--; if(vis[next.x]||next.x>maxn-1||next.x<0) continue; vis[next.x]=1; q.push(next); } } }}

 

转载于:https://www.cnblogs.com/qscqesze/p/4313020.html

你可能感兴趣的文章
几种排序方法
查看>>
查看数据库各表的信息
查看>>
第一阶段测试题
查看>>
第二轮冲刺第五天
查看>>
图片压缩
查看>>
Hadoop-2.6.5安装
查看>>
ES6思维导图
查看>>
第四周作业
查看>>
20151121
查看>>
线段重叠 (思维好题)
查看>>
Codeforces Round #413 C. Fountains (线段树的创建、查询、更新)
查看>>
SBuild 0.1.5 发布,基于 Scala 的构建系统
查看>>
WordPress 3.5 RC3 发布
查看>>
DOM扩展札记
查看>>
primitive assembly
查看>>
浅谈localStorage的用法
查看>>
Ad Exchange基本接口和功能
查看>>
Angular ui-router的常用配置参数详解
查看>>
软考知识点梳理--项目评估
查看>>
把特斯拉送上火星的程序员,马斯克!
查看>>