当前位置:网站首页>AcWing 4300. Two operations (minimum number of BFS searches)
AcWing 4300. Two operations (minimum number of BFS searches)
2022-07-06 22:56:00 【GHOSTANDBREAD】
Given a positive integer nn, We hope you can through a series of operations , Change it to another positive integer mm.
There are two kinds of operations :
- Multiply the current number by 22.
- Subtract... From the current number 11.
requirement , During the transformation , The number is always positive .
Please calculate , The minimum number of operations required .
Input format
a line , Two different positive integers nn and mm.
Output format
An integer , Indicates the minimum number of operations required .
Data range
front 66 Test points meet 1≤n,m≤101≤n,m≤10.
All test points meet 1≤n,m≤100001≤n,m≤10000.
sample input 1:
4 6
sample output 1:
2
sample input 2:
10 1
sample output 2:
9The code is as follows :
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int n, m;
struct point{
int x;
int step=0;
};
queue<point> r;
int vis[200010];
int main()
{
ios::sync_with_stdio(false);
cout.tie(NULL);
memset(vis, 0, sizeof(vis));
cin >> n >> m;
point start;
start.x = n;
start.step = 0;
vis[n] = 1;
r.push(start);
while(!r.empty())
{
int xx = r.front().x;
if(xx == m){
cout<< r.front().step;
break;
}
for(int i = 0; i < 2; i ++)
{
int y;
if(i == 0 && xx < m) y = 2 * xx;
else{
y = xx - 1;
if(y <= 0){
continue;
}
}
if(vis[y] == 0){
point temp;
temp.x = y;
temp.step = r.front().step + 1;
vis[y] = 1;
r.push(temp);
}
}
r.pop();
}
return 0;
}边栏推荐
- The ceiling of MySQL tutorial. Collect it and take your time
- MySQL中正则表达式(REGEXP)使用详解
- mysql查看表结构的三种方法总结
- dockermysql修改root账号密码并赋予权限
- 2014阿里巴巴web前实习生项目分析(1)
- Slide the uniapp to a certain height and fix an element to the top effect demo (organize)
- three. JS gorgeous bubble effect
- 华为云GaussDB(for Redis)揭秘第21期:使用高斯Redis实现二级索引
- #DAYU200体验官# 首页aito视频&Canvas绘制仪表盘(ets)
- Cocoscreator+typescripts write an object pool by themselves
猜你喜欢

自定义 swap 函数

AdaViT——自适应选择计算结构的动态网络

Aardio - integrate variable values into a string of text through variable names

关于声子和热输运计算中BORN电荷和non-analytic修正的问题

C three ways to realize socket data reception

云原生技术--- 容器知识点

Slide the uniapp to a certain height and fix an element to the top effect demo (organize)

UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
MySQL中正则表达式(REGEXP)使用详解

How to choose indoor LED display? These five considerations must be taken into account
随机推荐
How to confirm the storage mode of the current system by program?
Extern keyword
How to achieve text animation effect
Some suggestions for foreign lead2022 in the second half of the year
[compilation principle] LR (0) analyzer half done
C three ways to realize socket data reception
UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
rust知识思维导图xmind
How to choose the server system
UDP programming
使用云服务器搭建代理
C# 三种方式实现Socket数据接收
dockermysql修改root账号密码并赋予权限
Pytest unit test series [v1.0.0] [pytest execute unittest test case]
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
On the problems of born charge and non analytical correction in phonon and heat transport calculations
Jafka来源分析——Processor
OpenNMS separation database