CF Two Buttons (BFS)
2 seconds
256 megabytes
standard input
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?
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .
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.
4 6
2
10 1
9
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了,后来加入了剪枝,如果当前这个时间量搜过了那么就不再搜。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cctype>
#include <queue>
using namespace std; const int MAX = ;
bool VIS[MAX + ];
struct Node
{
int n;
int time;
}; void bfs(int,int);
int main(void)
{
int n,m; while(scanf("%d%d",&n,&m) != EOF)
{
if(n == m)
puts("");
else if(n > m)
printf("%d\n",n - m);
else
bfs(n,m);
}
} void bfs(int n,int m)
{
fill(VIS,VIS + MAX,false);
queue<Node> que;
Node first;
first.time = ;
first.n = n;
que.push(first); while(!que.empty())
{
Node cur = que.front();
que.pop();
for(int i = ;i < ;i ++)
{
Node next = cur;
if(!i)
{
next.n *= ;
next.time ++;
if(next.n > MAX)
continue;
}
else
{
next.n --;
next.time ++;
if(next.n <= )
continue;
}
if(next.n == m)
{
printf("%d\n",next.time);
return ;
} if(VIS[next.n])
continue;
VIS[next.n] = true; que.push(next);
}
} return ;
}
CF Two Buttons (BFS)的更多相关文章
- CF 520 B. Two Buttons(bfs)
/*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步.可以点红色按钮,蓝色按钮来改变数字,红色:*2,蓝色:-1,如果变成负数,就变成原来的数.CF 520 B. Two Buttons思 ...
- cf.295.B Two Buttons (bfs)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #295 (Div. 2)B - Two Buttons BFS
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- CF div2 D BFS
http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
- CodeForces 520B Two Buttons(用BFS)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- cf520B-Two Buttons 【BFS】
http://codeforces.com/contest/520/problem/B Two Buttons Vasya has found a strange device. On the fro ...
- Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )
B. Two Buttons time limit per test : 2 seconds memory limit per test :256 megabytes input :standard ...
- 【打CF,学算法——二星级】CF 520B Two Buttons
[CF简单介绍] 提交链接:Two Buttons 题面: B. Two Buttons time limit per test 2 seconds memory limit per test 256 ...
随机推荐
- Gym 100507H Pair: normal and paranormal (贪心)
Pair: normal and paranormal 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/H Description ...
- uniPanel特效
function beforeInit(sender){ sender.layout='accordion'; Ext.apply (sender, {title:'供应链',style:'text- ...
- 数据库防sql注入
- 代码静态分析工具——splint的学习与使用
引言 最近在项目中使用了静态程序分析工具PC-Lint,体会到它在项目实施中带给开发人员的方便.PC-Lint是一款针对C/C++语言.windows平台的静态分析工具,FlexeLint是针对其他平 ...
- TestDriven.NET – 快速入门
TestDriven.NET – 快速入门[译文] 介绍 这部分将提供一个快速的入门向导,在vs.NET的任何一个版本上面使用TestDriven.NET TDD(测试驱动开发)在你写你的代码之前,写 ...
- Nginx端口的修改
修改 nginx.conf 文件实现.在 Linux 上该文件的路径为 /usr/local/nginx/conf/nginx.conf,Windows 下 安装目录\conf\nginx.conf. ...
- 通过 监听器获取sessionId
package shopping; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpSess ...
- Wps的ppt里 让图片按顺序出现 就是点击一下 出现一张照片
基本操作能够用两种方法来实现: 方法一.每页幻灯片插入一张图片,幻灯片默认就是单击鼠标切换幻灯片的,所以不用再做其它设置. 方法二.在一页幻灯片中插入多张图片,全选图片(插入图片后,点击图片,Ctrl ...
- Python 动态语言
1.在C++中,Animal a = Person(); 这样写是不行的,因为a的内容不能使用Person的内容来填充. 2.在Python中,变量不需要声明,而且可以赋任何值.Python是如何做到 ...
- w3cmark前端精彩博文周报 10.27-11.2
w3cmark 官方Q群 145423956 | 官方微博 @w3cmark 这周周报来迟了,公司真的好忙!!!欢迎关注我们的微博 @w3cmark w3cmark推出每周精选前端博文推荐,通过阅读别 ...