好,童年阴影
查看原帖
好,童年阴影
54372
A_Đark_Horcrux楼主2020/11/3 20:22

18年十二月的时候发了一个求助帖子,当时就没搞懂

今天重新打了一遍,没打出来……

是个广搜

#include<cstdio>
using namespace std;
struct A{long long x,s;}q[1000010];
int T,n,x,y,h,t,s=99999999; bool b[1000010];
int main()
{
	scanf("%d",&T);
	while(T)
	{
		scanf("%d %d",&x,&y);
		if(x>=y) printf("%d\n",x-y);
		else
		{
			h=1,t=2,q[h].x=x,q[h].s=0;
			while(h<t)
			{
				b[q[h].x]=1;
				if(q[h].x==y) {printf("%d\n",q[h].s); break;}
				if(q[h].x*2<=2*y&&!b[q[h].x*2]) q[++t].x=q[h].x*2,q[t].s=q[h].s+1;
				if(q[h].x+1<=2*y&&!b[q[h].x+1]) q[++t].x=q[h].x+1,q[t].s=q[h].s+1;
				if(q[h].x-1>0&&!b[q[h].x-1]) q[++t].x=q[h].x-1,q[t].s=q[h].s+1;
				h++;
			}
			s=99999999; for(int i=0;i<=t+1;i++) q[i].x=q[i].s=b[i]=0;
		}
		T--;
	}	
	return 0;
}

这个程序会自动忽略太大的数据QAQ???

2020/11/3 20:22
加载中...