BFS写了个80求调(玄关)
  • 板块B3636 文字工作
  • 楼主zjy4
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/10/27 21:21
  • 上次更新2024/10/27 22:34:40
查看原帖
BFS写了个80求调(玄关)
1036776
zjy4楼主2024/10/27 21:21
#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);
}

咋办

2024/10/27 21:21
加载中...