当前位置:网站首页>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;
}
边栏推荐
- MySQL实现字段分割一行转多行的示例代码
- uniapp设置背景图效果demo(整理)
- docker中mysql开启日志的实现步骤
- Aardio - construct a multi button component with customplus library +plus
- mysql查看表结构的三种方法总结
- UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
- ICLR 2022 | pre training language model based on anti self attention mechanism
- Slide the uniapp to a certain height and fix an element to the top effect demo (organize)
- 存币生息理财dapp系统开发案例演示
- rust知识思维导图xmind
猜你喜欢
#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
自定义 swap 函数
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
Matlab tips (27) grey prediction
Introduction to network basics
Slide the uniapp to a certain height and fix an element to the top effect demo (organize)
Aardio - construct a multi button component with customplus library +plus
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
Custom swap function
Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned
随机推荐
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
How to choose the server system
How to achieve text animation effect
ThreadLocal详解
Puppeteer连接已有Chrome浏览器
DockerMySQL无法被宿主机访问的问题解决
树的先序中序后序遍历
监控界的最强王者,没有之一!
第十九章 使用工作队列管理器(二)
2022-07-05 use TPCC to conduct sub query test on stonedb
CUDA exploration
企业不想换掉用了十年的老系统
动作捕捉用于蛇运动分析及蛇形机器人开发
MySQL实现字段分割一行转多行的示例代码
【编译原理】做了一半的LR(0)分析器
Enterprises do not want to replace the old system that has been used for ten years
Traversal of a tree in first order, middle order, and then order
面试题:AOF重写机制,redis面试必问!!!
Comparison between variable and "zero value"