cf520B-Two Buttons 【BFS】
http://codeforces.com/contest/520/problem/B
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.
思路:bfs搜索。这里bool数组是关键。
代码:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <cstdlib> using namespace std; #define PI acos(-1.0)
#define EPS 1e-10
#define lll __int64
#define ll long long
#define INF 0x7fffffff struct node{
int t,cnt;
node(){}
node(int t,int cnt){
this->t=t;
this->cnt=cnt;
}
}tmp;
int n,m,ans=INF;
queue<node> q;
bool b[<<]; void bfs(); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
scanf("%d %d",&n,&m);
bfs();
printf("%d\n",ans);
return ;
}
void bfs(){
q.push(node(n,));
while(!q.empty()){
tmp=q.front();q.pop();
if(b[tmp.t]||tmp.t==) continue;
b[tmp.t]=;
if(ans<tmp.cnt) break;
if(tmp.t==m){
ans=min(ans,tmp.cnt);
}else if(tmp.t>m){
ans=min(ans,tmp.cnt+tmp.t-m);
}else{
q.push(node(tmp.t<<,tmp.cnt+));
q.push(node(tmp.t-,tmp.cnt+));
}
}
}
cf520B-Two Buttons 【BFS】的更多相关文章
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- 【bfs】拯救少林神棍(poj1011)
Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...
- 【bfs】Knight Moves
[题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...
- 【bfs】1252 走迷宫
[题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
- 【bfs】献给阿尔吉侬的花束
[题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...
- 【bfs】迷宫问题
[题目描述] 定义一个二维数组: int maze[5][5] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; 它表示一个迷 ...
- 【bfs】仙岛求药
[题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...
- 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz
最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...
- poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
随机推荐
- 各开源 bbs 程序比较
主要是集中在 php 开源轻巧的程序. 搜索到一个逼乎的一个帖子:https://www.zhihu.com/question/20655704 ,顺藤摸瓜 下. carbon forum 第一个测试 ...
- BZOJ5090: [Lydsy1711月赛]组题(01分数规划)
5090: [Lydsy1711月赛]组题 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 785 Solved: 186[Submit][Status ...
- Js 图片轮播渐隐效果
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 用 dotTrace 进行性能分析时,各种不同性能分析选项的含义和用途
对 .NET 程序进行性能分析,dotTrace 能应对绝大多数的场景.在开启一个进程进行性能分析之前,我们会看到一些性能分析选项(Profiler Options).本文将介绍这几个选项的含义,并用 ...
- test20181024 kun
题意 分析 考场做法 任意状态只有两种决策,进栈或出栈. 由于元素互不相同,所以选择的关键在于栈顶元素是否在它和带插入元素组成的集合中是最大的. 用stack和set维护.时间复杂度\(O(n \lo ...
- vue路由初始化路转
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Oracle单表去重复(二)
Oracle单表去重 去重有两层含义,一:是记录完全一样.二:是符合一定条件的认为是重复. 根据表的数量,去重可划分为:单表去重和多表关联去重. 对于去重,一般最容易想到的是用distinct,而 ...
- 关于oracle的profiles
一.关于oracle的profiles profiles文件是口令和资源限制的配置集合,包括CPU的时间.I/O的使用.空闲时间.连接时间.并发会话数量.密码策略等对于资源的使用profile可以做到 ...
- Beautiful and Powerful Correlation Tables in R
Another correlation function?! Yes, the correlation function from the psycho package. devtools::inst ...
- 黄聪:V2010中C#实现友好的等待任务完成时,出现的多线程悬浮窗体
实现效果如下: 项目已经打包后,大家直接下载吧:[HCWaitForm.rar]