BFS,T了第2个,求优化
查看原帖
BFS,T了第2个,求优化
1284088
meifan666楼主2024/12/8 15:31
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mod 32768
int n,a[32800],vis[32800];
struct in{
	int x,v;
};
queue<in>p;
signed main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		memset(vis,0,sizeof(vis));
		while(!p.empty())p.pop();
		p.push({a[i],0});
		while(!p.empty()){
			in t=p.front();p.pop();
			if(t.x==0){
				printf("%d ",t.v);
				break;
			}
			vis[t.x]=1;
			if(!vis[(t.x+1)%mod]){
				p.push({(t.x+1)%mod,t.v+1});
				vis[(t.x+1)%mod]=1;
			}
			if(!vis[(t.x*2)%mod]){
				p.push({(t.x*2)%mod,t.v+1});
				vis[(t.x*2)%mod]=1;
			}
		}
	}
	return 0;
}
2024/12/8 15:31
加载中...