Codeforces Round #295 (Div. 2)B - 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.
两种操作:
1.乘以2
2.减一
然后直接跑一发bfs就好啦
struct node
{
int x;
int step;
};
int vis[maxn];
int n,m;
int main()
{
cin>>n>>m;
queue<node> q;
q.push({n,});
vis[n]=;
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.x==m)
{
cout<<now.step<<endl;
return ;
}
REP(i,)
{
node next=now;
next.step++;
if(i)
{
next.x*=;
if(vis[next.x]||next.x>maxn-||next.x<)
continue;
vis[next.x]=;
q.push(next);
}
else
{
next.x--;
if(vis[next.x]||next.x>maxn-||next.x<)
continue;
vis[next.x]=;
q.push(next);
}
}
}
}
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 ...
- 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 #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> # ...
- codeforces 521a//DNA Alignment// Codeforces Round #295(Div. 1)
题意:如题定义的函数,取最大值的数量有多少? 结论只猜对了一半. 首先,如果只有一个元素结果肯定是1.否则.s串中元素数量分别记为a,t,c,g.设另一个串t中数量为a',t',c',g'.那么,固定 ...
- Codeforces Round #295 (Div. 2)C - DNA Alignment 数学题
C. DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- ARC073E Ball Coloring
Problem AtCoder Solution 把点映射至二维平面,问题就变成了给定 \(n\) 个点,可以把点对 \(y=x\) 对称,求覆盖所有点的最小矩形面积. 可以先把所有点放到 \(y=x ...
- scala下实现actor多线程基础
package cn.huimin.test import akka.actor._ object NewWrite extends App{ private val system = ActorSy ...
- MongoDB(3.6.3)的用户认证初识
Windows 10家庭中文版,MongoDB 3.6.3, 前言 刚刚安装好了MongoDB,启动了服务器-mongod命令,启动了MongoDB shell-mongo命令,不过,全程都没有使用u ...
- log4j与commons-logging slf4j的关系
1. slf4j 他只提供一个核心slf4j api(就是slf4j-api.jar包),这个包只有日志的接口并没有实现 所以如果要使用就得再给它提供一个实现了些接口的日志包, ...
- openlayers常用操作
1.坐标转换 根据当前坐标系与目标坐标系进行转换ol.proj.transform(coordinate, source, destination); //coordinate:数组:source: ...
- Service(一):认识service、绑定Service
Activity是与用户打交道的,而Service是在后台运行的. 这个程序介绍了下如何启动和停止一个Service,以及在后台打印消息,我添加了一些注释. 在activity_main中将布局改为线 ...
- JS正则表达式方法
使用正则表达式的主要有match,exec,test 1.正则表达式方法test测试给定的字符串是否满足正则表达式,返回值是bool类型的,只有真和假. var user_code = $(" ...
- Python3中的yield from语法
Python3中的yield from语法 by Kay Zheng Tags: python, 协程, generator 30 March 2014 2016-2-23 更新 這篇文章是兩年前寫的 ...
- Pg188-2 覆盖 向上转型
package org.hanqi.array; public class DongWu { private String name; private String color; public Str ...
- HTML/CSS权值继承
<style type="text/css">p{color:red;}.first{color:green;}/*因为权值高显示为绿色*/ span{color:pi ...