博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF Two Buttons (BFS)
阅读量:6647 次
发布时间:2019-06-25

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

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 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 10 const int MAX = 10000;11 bool VIS[MAX + 5];12 struct Node13 {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",n - m);30 else31 bfs(n,m);32 }33 }34 35 36 void bfs(int n,int m)37 {38 fill(VIS,VIS + MAX,false);39 queue
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 else60 {61 next.n --;62 next.time ++;63 if(next.n <= 0)64 continue;65 }66 if(next.n == m)67 {68 printf("%d\n",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 }

 

转载于:https://www.cnblogs.com/xz816111/p/4399703.html

你可能感兴趣的文章
深拷贝和浅拷贝
查看>>
java版sqlhelper(转)
查看>>
android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK
查看>>
我的第一本著作:Spark技术内幕上市!
查看>>
现实世界的Windows Azure:采访Gridsum的Sr.业务发展总监Yun Xu
查看>>
公开发布版的Windows Azure 基础结构服务中的 SQL Server – 文档和最佳实践(已更新),还有即将发布的博客...
查看>>
UVa 494 - Kindergarten Counting Game
查看>>
java中IO操作
查看>>
Python 值传递和引用传递
查看>>
hdu4405Aeroplane chess 概率dp水题
查看>>
jq查找父类元素三个函数的区别
查看>>
1.27eia原油
查看>>
vue loading 插件编写与实战
查看>>
Linux I/O多路转接之select函数
查看>>
Android深度探索第二章总结
查看>>
matlab练习程序(单源最短路径Bellman-Ford)
查看>>
深入理解Java的接口和抽象类
查看>>
JavaScript 简介
查看>>
随写内部类
查看>>
【WEB】Tomcat基础使用知识
查看>>