CodeForces 520B 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.
题目很容易理解,从一个数,到另外一个数,在只有两步操作的情况下,用至少几步能到达。
开始看人家标签,用dfs想些,搞了半天不会。。。然后想到用最少步,bfs应该可以。就试着写了。
写了好几遍,人家数据范围是 一万, 我要开 十万 的数组才能过,这肯坑定是问题。果然, 在bfs里控制访问 now值二倍时,有问题,剪枝没剪对。
///当 n > m 时,并没有比 -1 更好的办法
///当 n < m 时, 就需要去寻找答案 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std; const int max_size = + ;
int step[max_size*];
int vis[max_size*]; int bfs(int n, int m)
{
int now;
queue<int> que;
que.push(n);
vis[n] = ;
step[n] = ; while(que.size())
{
now = que.front();
if(now == m)
{
return step[now];
}
que.pop();
if(now- > && !vis[now-])
{
que.push(now-);
vis[now-] = ;
step[now-] = step[now] + ;
}
if(now <= m && !vis[now*])
{
que.push(now*);
vis[now*] = ;
step[now*] = step[now] + ;
}
}
} int main()
{
int n, m;
cin >> n >> m;
memset(step, , sizeof(step));
memset(vis, , sizeof(vis)); if(n > m)
cout << n - m << endl;
else
{
cout << bfs(n, m) << endl;
}
return ;
}
CodeForces 520B Two Buttons(用BFS)的更多相关文章
- CodeForces 520B Two Buttons
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Vasya ...
- Codeforces.520B.Two Buttons(正难则反)
题目链接 \(Description\) 给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2.求最少需要多少次可以使\(n\)等于\(m\). \(Solution\) 暴力连边BF ...
- 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 ...
- 【codeforces 520B】Two Buttons
[题目链接]:http://codeforces.com/contest/520/problem/B [题意] 给你一个数n; 对它进行乘2操作,或者是-1操作; 然后问你到达m需要的步骤数; [题解 ...
- Codeforces 520B:Two Buttons(思维,好题)
题目链接:http://codeforces.com/problemset/problem/520/B 题意 给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m 思路 ...
- cf520B-Two Buttons 【BFS】
http://codeforces.com/contest/520/problem/B Two Buttons Vasya has found a strange device. On the fro ...
- codeforces 520 Two Buttons
http://codeforces.com/problemset/problem/520/B B. Two Buttons time limit per test 2 seconds memory l ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- CodeForces 540C Ice Cave (BFS)
http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS Memory Limit:262 ...
随机推荐
- 优化Table View
优化Table View Table view需要有很好的滚动性能,不然用户会在滚动过程中发现动画的瑕疵. 为了保证table view平滑滚动,确保你采取了以下的措施: 正确使用`reuseIden ...
- 使用ASP.NET WEB API构建基于REST风格的服务实战系列教程(一)——使用EF6构建数据库及模型
系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 使用Entity Framework Code First模式构建数据库对象 已经决定使用EF C ...
- Redis主-从部署实践
0. 前言 这篇文章简要介绍Redis的主从部署,实现了一主二从,使用两个哨兵监控,以实现简单的HA,其中从库作为备机. 1. 部署 这里有三台服务器,其中239主机上的Redis作为主库,其余两个作 ...
- 如何在网页中添加“QQ交流”
今天在撸码时,想到这个问题,有些网页中会有诸如,那么如何在网页添加"QQ交谈"? 第一步.登录QQ: 第二步.打开网页:QQ推广,启用QQ通讯组件: 第三步.选择组件样式,设置提示 ...
- [译]git的那些flag
git add -p console有一个交互式的界面(如下图),让你一个一个文件的选择是add还是不add.注意这些文件必须是tracked过的, 也就是说如果你的新的文件从来没有add过,那么他不 ...
- PHP获取当前域名$_SERVER['HTTP_HOST']和$_SERVER['SERVER_NAME']的区别
开发站群软件,用到了根据访问域名判断子站点的相关问题,PHP获取当前域名有两个变量 $_SERVER['HTTP_HOST'] 和 $_SERVER['SERVER_NAME'],两者的区别以及哪个更 ...
- no screens found! ubuntu进不了图形界面了
no screens found! ubuntu进不了图形界面了 结果是没装显卡 startx error. reinstall xorg, x server doesn't work. driver ...
- PYTHON 写函数,计算传入字符串中【数字、字母、空格、以及其他的个数】
def func1(s): al_num = 0 spance_num = 0 digit_num = 0 others_num = 0 for i in s: if i.isdigit(): # i ...
- js框架设计1.3数组化
这一节从作者哪里学来了[].slice.call([],0,1);这个方法第一个参数可是是字符串可以是数组或其他,第2个是数组截取位置的开始位置,第3个是终止位置. 作者说这个方法不兼容旧版本ie的, ...
- video.js使用教程API
videojs就提供了这样一套解决方案,他是一个兼容html5的视频播放工具,早期版本兼容所有浏览器,方法是:提供三个后缀名的视频,并在不支持html5的浏览器下生成一个flash的版本. 最新的3. ...