1 条题解

  • 0
    @ 2024-12-24 10:06:20

    C++ :

    
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    struct node{int a[4],dep;}list[41000];
    int nn[4],n,ed,head,tail;
    bool myhash[41000];
    
    int Fkangtuo(node tno)
    {
    	return tno.a[1]*101+tno.a[2]*102+tno.a[3];
    }
    bool OK(node tno)
    {
    	if(tno.a[1]==n||tno.a[2]==n||tno.a[3]==n) return true;
            return false;
    }
    node dao(int x,int y)
    {
    	node tno=list[head];
    	int zz=tno.a[x]+tno.a[y];
    	if(zz>nn[y])
        {
            tno.a[y]=nn[y];
            tno.a[x]=zz-nn[y];
        }
    	else {tno.a[y]=zz;tno.a[x]=0;}
    	tno.dep++;
    	return tno;
    }
    int main()
    {
    	bool bo;
    	while(scanf("%d%d%d%d",&nn[1],&nn[2],&nn[3],&n)!=EOF)
    	{
    		bo=false;
    		memset(myhash,false,sizeof(myhash));
    		if(nn[1]==n) {printf("yes\n0\n");continue;}
    		list[1].a[1]=nn[1];list[1].a[2]=list[1].a[3]=0;
    		myhash[Fkangtuo(list[1])]=true;
    		head=tail=1;
    		while(head<=tail)
    		{
                node tno;
    			for(int i=1;i<=3;i++)
    			{
                    if(!bo)
    				for(int k=1;k<=3;k++)
    					if(i!=k)
    					{
    						tno=dao(i,k);
    						int x=Fkangtuo(tno);
    						if(!myhash[x])
    						{
    							myhash[x]=true;
    							list[++tail]=tno;
    							if(OK(tno))
                                    {
                                        bo=true;
                                        printf("yes\n%d\n",list[tail].dep);
                                        break;
                                    }
    						}
    					}
                }
    			head++;
    		}
    		if(!bo) printf("no\n");
    	}
    }
    
    
    • 1

    信息

    ID
    2705
    时间
    2000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者