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> # ...
随机推荐
- IDEA连接linux服务器
idea连接linux(完成了xshell和xftp连接linux的功能,可以直接卸载这俩了..) File->settings->Deployment左侧加号添加 选择传输类型ftp或者 ...
- Smart3D系列教程8之 《模型合并——相邻地区多次建模结果合并》
迄今为止,Wish3D已经出品推出了7篇系列教程,从倾斜摄影的原理方法.采集照片的技巧.Smart3D各模块的功能应用.小物件的照片重建.大区域的地形重建到DSM及正射影像的处理生产,立足于建模软件的 ...
- C#串口通信发送数据
1 发送数据 需要2个串口 http://www.openedv.com/thread-228847-1-1.html 下载源文件 File_Protocol_Test.rar
- SWTBOK測试实践系列(5) -- 项目中使用手动和自己主动化的策略
手动測试和自己主动化測试永远是一个非常热门的话题.自己主动化也一直被人们捧上神坛.自己主动化測试和手动測试从技术上来说本质事实上都是測试用例设计.仅仅只是终于形式一个是人工运行,一个是代码运行罢了.这 ...
- javascript 转义函数
// 字符转义 html2Escape(sHtml) { return sHtml.replace(/[<>&"]/g, function(c) { return { ' ...
- Word Ladder II——找出两词之间最短路径的所有可能
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 使用Android注解来改善代码
昨晚看到一篇好文章.然后是英文的.所以决定翻译分享给大家.这是原文链接:http://www.michaelevans.org/blog/2015/07/14/improving-your-code- ...
- mysql 执行sql文件的方法
http://philos.iteye.com/blog/162051 实战代码: #mysql导入mysql -um4n -p01D060A476642BA8335B832AC5B211F22 ...
- css3: background-image使用多个背景图像
CSS3 允许元素使用多个背景图像. background-image: url(img/ic_ms.png),url(img/icon_dutyfree_invite.png); <!DOCT ...
- PHP几种抓取网络数据的常见方法
//本小节的名称为 fsockopen,curl与file_get_contents,具体是探讨这三种方式进行网络数据输入输出的一些汇总.关于 fsockopen 前面已经谈了不少,下面开始转入其它. ...