#include<bits/stdc++.h>
using namespace std;
int n,vis[1100000];
void bfs(int x)
{
for(int i=1;i<=n;i++)
{
vis[i]=-1;
}
queue<int>q;
q.push(x);
vis[x]=0;
while(q.size())
{
int t=q.front();
q.pop();
int z=t+1;
if(z<=n && vis[z]==-1)
{
vis[z]=vis[t]+1;
q.push(z);
}
z=t-1;
if(z>=1 && vis[z]==-1)
{
vis[z]=vis[t]+1;
q.push(z);
}
z=t*2;
if(z<=n && vis[z]==-1)
{
vis[z]=vis[t]+1;
q.push(z);
}
}
}
int main()
{
cin>>n;
bfs(1);
cout<<vis[n];
}1
文章信息与版本
文章信息
- 收录
- 2025年12月12日
- 更新
- 2026年5月23日
- 篇幅
- 约 1 分钟