bfs(洛谷b3626

include<bits/stdc++.h>

using namespace std;
const int N=1000010;
int n,ans;
int step;
queue<pair<int,int> > que;
bool st[N];

int main() {
cin>>n;
ans=100001;
que.push({1,0});
while(!que.empty()){
pair<int,int> f=que.front();
que.pop();
int x=f.first,cost =f.second;
if(x<=0 || x>n) continue;
if(st[x]) continue;
st[x]=1;
if(x==n){
cout<<cost;
return 0;
}
que.push({x-1,cost+1});
que.push({x+1,cost+1});
que.push({x*2,cost+1});
}
return 0;
}

posted on 2025-02-24 10:36  下头小美  阅读(93)  评论(0)    收藏  举报