Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )
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.
题目和算法分析:
在每组测试数据里,输入n和m的值,求从n变到m最少所需要的变换次数。每次对于n的操作可以是:*2 或者 -1。
比如第一组测试数据:要从n=4变到m=6,需要将n=4-1=3, 再3*2=6=m,这样最小且只需要2步。
通过bfs进行广度优先搜索,注意已经搜索过的数值不进行第二搜索,所以需要标记数组。此外还需要一个记录步数的数组。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#define N 10000+1000
#define M 10000 using namespace std; int n, m;
bool vis[N];
int dis[N]; void bfs()
{
queue<int>q;
q.push(n);
vis[n]=true;
dis[n]=;
int cur;
while(!q.empty())
{
cur=q.front(); q.pop();
if(cur==m)
{
printf("%d\n", dis[cur]);
return ;
}
else
{
if(cur->= && cur<=M && vis[cur-]==false)
{
vis[cur-]=true;
q.push(cur-);
dis[cur-]=dis[cur]+;
}
if(cur*>= && cur*<=M && vis[cur*]==false)
{
vis[cur*]=true;
q.push(cur*);
dis[cur*]=dis[cur]+;
}
}
}
} int main()
{
scanf("%d %d", &n, &m);
memset(dis, , sizeof(dis));
memset(vis, false, sizeof(vis));
bfs();
return ;
}
Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )的更多相关文章
- 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 ...
- Codeforces Round #295 (Div. 2) B. Two Buttons
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #295 (Div. 2) B. Two Buttons 520B
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons
题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...
- Codeforces Round #295 (Div. 2) B. Two Buttons (DP)
题意:有两个正整数\(n\)和\(m\),每次操作可以使\(n*=2\)或者\(n-=1\),问最少操作多少次使得\(n=m\). 题解:首先,若\(n\ge m\),直接输出\(n-m\),若\(2 ...
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)
Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...
- Codeforces Round #295 (Div. 2)
水 A. Pangram /* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> # ...
随机推荐
- SPOJ 7001 VLATTICE【莫比乌斯反演】
题目链接: http://www.spoj.com/problems/VLATTICE/ 题意: 1≤x,y,z≤n,问有多少对(x,y,z)使得gcd(x,y,z)=1 分析: 欧拉搞不了了,我们用 ...
- 如何通过ShareSDK的 Unity3D快速接入Android/iOS分享与授权
Unity3D是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎:在游戏 ...
- FMDB使用Cached Statement功能
FMDB使用Cached Statement功能 在FMDB中,Cached Statement功能是一种提高SQLite数据库访问的技术.在SQLite中,所有的SQL语句都会被编译,形成预处理 ...
- JS没有contains方法,可以用indexof实现
我们很多时候会不自觉的在js代码里对一个字符串进行如下操作: str.contains("substr"); 但是js里面没有这个方法去判断字符串str是不是包含substr,而j ...
- vue框架及其
Vue常用UI框架 PC端: 1. ElementUI:http://element-cn.eleme.io/#/zh-CN 2. iView:https://www.iviewui.com/ 3. ...
- andriod打开摄像头和打开相册
package com.example.yanlei.picture; import android.support.v7.app.AppCompatActivity; import android. ...
- 牛人写的facebook优化php来龙去脉
背景 HHVM 是 Facebook 开发的高性能 PHP 虚拟机,宣称比官方的快9倍,我很好奇,于是抽空简单了解了一下,并整理出这篇文章,希望能回答清楚两方面的问题: HHVM 到底靠谱么?是否可以 ...
- Go -- pprof协程监控
go中有pprof包来做代码的性能监控,在两个地方有包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装了一 ...
- 如何在Linux中使用sFTP上传或下载文件与文件夹
如何在Linux中使用sFTP上传或下载文件与文件夹 sFTP(安全文件传输程序)是一种安全的交互式文件传输程序,其工作方式与 FTP(文件传输协议)类似. 然而,sFTP 比 FTP 更安全;它通过 ...
- electron 缓存目录 禁用缓存
C:\Users\Administrator\AppData\Roaming\linksame // 禁用缓存 app.commandLine.appendSwitch("--disable ...