一直输出0
查看原帖
一直输出0
1495466
LINYUHENG2楼主2024/12/25 20:31
#include <bits/stdc++.h>
#define MAXN 1000000
using namespace std;

int t[MAXN+1];
int vis[MAXN+1];
int n;
queue<int> q;
void bfs(){
	q.push(n);
	vis[n]=1;
	while(!q.empty()){
		int pos=q.front();
		q.pop();
		if(pos==n){
			cout<<t[pos];
			return ;
		}
		for(int i=0;i<3;i++){
			int next_pos;
			if(i==0) next_pos=pos+1;
			if(i==1) next_pos=pos-1;
			if(i==2) next_pos=pos*2;
			if(next_pos>=0&&next_pos<=n&&!vis[next_pos]){
				vis[next_pos]=1;
				t[next_pos]=t[pos]+1;
				q.push(next_pos);
			}
		}
	}
}

int main(){
    cin>>n;
    bfs();
    return 0;
}
2024/12/25 20:31
加载中...