社区讨论

BFS写了个80求调(玄关)

B3636文字工作参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@m2rmebgk
此快照首次捕获于
2024/10/27 21:23
去年
此快照最后确认于
2025/11/04 15:51
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
queue<int> a,cs;
int vis[1000001];
void bfs(int n){
	a.push(1),cs.push(0);
	while(!a.empty()){
		int nx=a.front()+1;
		if(nx<=n&&vis[nx]==0) {
			if(nx==n){
				cout<<cs.front()+1;
				return;
			}
			vis[nx]=1;
			a.push(nx);
			cs.push(cs.front()+1);
		}
		nx=a.front()*2;
		if(nx<=n&&vis[nx]==0) {
			if(nx==n){
				cout<<cs.front()+1;
				return;
			}
			vis[nx]=1;
			a.push(nx);
			cs.push(cs.front()+1);
		}
		a.pop();
		cs.pop();
	}
}
int main(){
	int n;
	cin>>n;
	bfs(n);
}
咋办

回复

3 条回复,欢迎继续交流。

正在加载回复...