当前位置:网站首页>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;
}
边栏推荐
- [IELTS speaking] Anna's oral learning record part1
- MySQL中正则表达式(REGEXP)使用详解
- Extern keyword
- 如何实现文字动画效果
- Windows auzre background operation interface of Microsoft's cloud computing products
- Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
- 案例推荐丨安擎携手伙伴,保障“智慧法院”更加高效
- Rust knowledge mind map XMIND
- three. JS gorgeous bubble effect
- DR-Net: dual-rotation network with feature map enhancement for medical image segmentation
猜你喜欢
MySQL中正则表达式(REGEXP)使用详解
Case recommendation: An Qing works with partners to ensure that the "smart court" is more efficient
云原生技术--- 容器知识点
Dayu200 experience officer homepage AITO video & Canvas drawing dashboard (ETS)
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
How to confirm the storage mode of the current system by program?
ICLR 2022 | pre training language model based on anti self attention mechanism
AdaViT——自适应选择计算结构的动态网络
UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
View
随机推荐
BasicVSR_ Plusplus master test videos and pictures
Custom swap function
Machine test question 1
使用云服务器搭建代理
MySQL教程的天花板,收藏好,慢慢看
How to confirm the storage mode of the current system by program?
How big is the empty structure?
让我们,从头到尾,通透网络I/O模型
DockerMySQL无法被宿主机访问的问题解决
#DAYU200体验官# 首页aito视频&Canvas绘制仪表盘(ets)
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
CRMEB商城系统如何助力营销?
允许全表扫描 那个语句好像不生效set odps.sql.allow.fullscan=true;我
视图(view)
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
「小程序容器技术」,是噱头还是新风口?
Introduction to network basics
MATLAB小技巧(27)灰色预测
Precise drag and drop within a contentable