4 条题解

  • -3
    @ 2023-12-3 16:15:09
    #include<bits/stdc++.h>
    using namespace std;
    int a[100001],que[100001][3];
    int main()
    {
    	int n,k,head,tail,xx,t;
    	cin>>n>>k;
    	if(n==k)
    	{
    		cout<<0<<endl;
    		return 0;
    	}
    	head=0;
    	tail=1;
    	que[1][0]=n;
    	que[1][1]=0;
    	a[n]=1;
    	while(head<tail)
    	{
    		head++;
    		xx=que[head][0];
    		for(int i=1;i<=3;i++)
    		{
    			if(i==1) t=xx+1;
    			if(i==2) t=xx-1;
    			if(i==3) t=2*xx;
    			if(t>=0 && t<=100000&&a[t]==0)
    			{
    				tail++;
    				que[tail][0]=t;
    				que[tail][1]=que[head][1]+1;
    				a[t]=1;
    				if(t==k)
    				{
    					cout<<que[tail][1];
    					return 0;
    				}
    			}
    		}
    	 } 
     return 0; 
    }
    

    信息

    ID
    1637
    时间
    1000ms
    内存
    256MiB
    难度
    5
    标签
    递交数
    221
    已通过
    87
    上传者