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 ...
随机推荐
- Ubuntu 16.04系统开机紫屏的解决办法
具体症状为卡在开机界面,按任何键都无反应. 网上查看了几篇文章 ,如下: 解决:ubuntu16.04启动时长时间停留在紫屏或跳文本的黑屏界面 Ubuntu16.04显卡驱动 电源管理 里面提到的开机 ...
- .net 应用程序 发布上线注意事项
生产环境发布时,对应的程序目录必须新建当日rar压缩包进行备份生产环境数据库发布时,必须创建存储过程的副本sql用于回滚,操作方式:F7调出对象资源管理器详细信息->选中所有存储过程->编 ...
- Linux:shell脚本字符显示特殊颜色效果
shell脚本字符显示颜色和特殊效果 (一) 颜色显示 1)字符颜色显示 #!/bin/bash #字符颜色显示 #-e:允许echo使用转义 #\[:开始位 #\[0m:结束位 #\033等同于\e ...
- 手机dp和px的转换
1dp 0.75px ----> 320*240 1dp 1px ----->480*320 1dp 1.5px ----->800*480 4 ...
- 20155224 2016-2017-2 《Java程序设计》第8周学习总结
20155224 2016-2017-2 <Java程序设计>第X周学习总结 教材学习内容总结 第十四章 NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以设定缓冲区 ...
- SM2的非对称加解密java工具类
maven依赖 <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov- ...
- C语言--第四次作业
作业要求一 (70分) 实践最简答的项目wordcount,必须完成其中的基本功能,若可以完成其他功能给予加分.完成后请将你的设计思路.主要代码写在本次作业博客里. 真的迷茫<(_ _)> ...
- Mac触摸板没有弹性了
关机后,同时按启动键,空格键左边的option,command键还有p和r,听到开机声音响四声后再松开.一定要同时按!然后触摸板就可以用了. (转自知乎)
- nginx 正则
syntax: location [=|~|~*|^~] /uri/ { … }语法:location [=|~|~*|^~] /uri/ { … } 优先级从高到低:=.~ .~*.^~.空 def ...
- DCI改进,发布后作业乱码不能打开
1.发布后作业不能打开问题,找到com.comsys.net.cn.dci.ui.dialog.PublishesDialog 的960行,改为这样: //以前没有指定文件编码前,采用系统默认编码 / ...