BFS(悬关求调)
查看原帖
BFS(悬关求调)
1069781
Lzj_qwq楼主2024/10/2 12:17
#include<cstdio>
#include<queue>
using namespace std;
struct point{
	int n,step;
};
queue<point> q;
int main(){
	int T,n,k; point u;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&k);
		q.push((point){n,0});
		while(1){
			u=q.front(); q.pop();
			if(u.n==k) {printf("%d",u.step);break;}
			if(u.n+1<=k) q.push((point){u.n+1,u.step+1});
			if(u.n-1>0) q.push((point){u.n-1,u.step+1});
			if(u.n*2<=1e6) q.push((point){u.n*2,u.step+1});
		}
	}
	return 0;
}
2024/10/2 12:17
加载中...