各位 dalao ,帮帮 本蒟蒻吧!我真的没办法了
#include<iostream>
#include<queue>
using namespace std;
const int dx[]={0,-1,1};
struct node
{
int x;
int step;
};
queue <node> q;
long long n;
bool vis[1];
void bfs()
{
while(q.size())
{
node now=q.front();
q.pop();
if(now.x==n)
{
cout<<now.step;
exit(0);
}
for(int i=1;i<=3;i++)
{
int xx=now.x+dx[i];
if(xx>=1&&xx<=n&&vis[xx]==0)
{
node tool;
tool.x=xx,tool.step=now.step+1;
q.push(tool);
}
}
int xx=now.x*2;
if(xx>=1&&xx<=n&&vis[xx]==0)
{
node tool;
tool.x=xx,tool.step=now.step+1;
q.push(tool);
}
}
}
int main()
{
cin>>n;
node tool;
tool.x=1,tool.step=0;
vis[1]=1;
q.push(tool);
bfs();
return 0;
}