当前位置:网站首页>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:
9
The 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;
}
边栏推荐
- uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
- 「小程序容器技术」,是噱头还是新风口?
- Demonstration of the development case of DAPP system for money deposit and interest bearing financial management
- 【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
- The application of machine learning in software testing
- CocosCreator+TypeScripts自己写一个对象池
- The ceiling of MySQL tutorial. Collect it and take your time
- UVa 11732 – strcmp() Anyone?
- Slide the uniapp to a certain height and fix an element to the top effect demo (organize)
- CRMEB商城系统如何助力营销?
猜你喜欢
MySQL实现字段分割一行转多行的示例代码
Cocoscreator+typescripts write an object pool by themselves
C three ways to realize socket data reception
Config:invalid signature solution and troubleshooting details
docker中mysql开启日志的实现步骤
儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程
CocosCreator+TypeScripts自己写一个对象池
【全网首发】Redis系列3:高可用之主从架构的
ICLR 2022 | pre training language model based on anti self attention mechanism
Bipartite graph determination
随机推荐
On file uploading of network security
UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
视图(view)
Void keyword
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
Improving Multimodal Accuracy Through Modality Pre-training and Attention
案例推荐丨安擎携手伙伴,保障“智慧法院”更加高效
Extern keyword
2022-07-05 use TPCC to conduct sub query test on stonedb
MySQL教程的天花板,收藏好,慢慢看
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
华为云GaussDB(for Redis)揭秘第21期:使用高斯Redis实现二级索引
Aardio - does not declare the method of directly passing float values
POJ 1094 sorting it all out
Adavit -- dynamic network with adaptive selection of computing structure
uniapp设置背景图效果demo(整理)
[untitled]
浅谈网络安全之文件上传
Interview question: AOF rewriting mechanism, redis interview must ask!!!