0分RE求调
查看原帖
0分RE求调
1028443
harry2768楼主2024/8/29 15:34
#include<bits/stdc++.h>
using namespace std;

int mh[3]={1,-1,2};
int n,x,y,head,tail;
bool f[100010]={false};

struct c{
	int h,steps;
}t[100010];

void bfs(){
	head=0,tail=1;
	t[1].h=x,t[1].steps=0;
	while(head<=tail){
		head++;
		int h=t[head].h;
		for(int i=1;i<=3;i++){
			int nh;
			if(i==3) {
				nh=h*2;
			}
			else 
				nh=h+mh[i];
				
			if(f[nh]==false){
				f[nh]=true;
				tail++;
				t[tail].h=nh;
				t[tail].steps=t[head].steps+1;
				if(nh==y){
					cout<<t[tail].steps;
					return ;
				}
			}
		}
	}
}

int main(){
	cin>>n;
	while(n--){
		cin>>x>>y;
		bfs();
	}
	return 0;
}
2024/8/29 15:34
加载中...