POJ-1190 Birthday Cake (DFS+Pruning)

POJ-1190 Birthday Cake

Description:
July 17th is Mr. W's birthday. For this purpose, ACM-THU will make an M-layer birthday cake with a volume of Nπ, each layer is a cylinder.
Suppose the i-th layer of cake from bottom to top (1 <= i <= M) is a cylinder with radius Ri and height Hi. When i <M, it is required that Ri> Ri+1 and Hi> Hi+1.
Due to the need to put cream on the cake, in order to save money as much as possible, we hope that the area Q of the outer surface of the cake (except the bottom surface of the bottom layer) is the smallest.
Let Q = Sπ
, please program to find out the cake making plan (appropriate values ​​of Ri and Hi) for the given N and M, so that S is the smallest.
(Except Q, all the above data are positive integers)
Input
has two lines, the first line is N (N <= 10000), which means the volume of the cake to be made is Nπ; the second line is M (M <= 20), Indicates that the number of layers of the cake is M.
Output
is only one line and is a positive integer S (if there is no solution, then S = 0).
Sample Input

100
2

Sample Output

68

Hint
cylindrical formula
Volume V = πR2H
side area A'= 2πRH
bottom area A = πR2
Sponsor
question analysis: Direct search is obviously unrealistic, we need pruning, then we can think from the following aspects:

  1:当前面积比最小面积大,可以剪枝

  2:当前面积加上剩下最小面积大于最小面积,可以剪枝

  3:若剩余的体积比做最大的蛋糕的体积还要大,那也可以剪枝了,

code show as below:

#include <stdio.h> 
void DFS(int e,int V,int C,int R,int H);
int n,m,k=0; 
int main()
{
    
    
    scanf("%d%d",&n,&m);
    DFS(m,n,0,100,1000);
    printf("%d",k);
 return 0;
}
void DFS( int e, int V, int C, int R, int H)
{
    
    
	int r,h,v,c;
	if(V<0) 
	{
    
    
		return;
	}
	if(!e) 
	{
    
    
        if(!V&& (C<k||!k)) 
        {
    
    
        	k=C;
		}	
        return;
    }
	if(k&&C>k) 
	{
    
    
		return;
	}
    if(e*(R-1)*(R-1)*(H-1)<V &&e!=m) 
    {
    
    
		return;
	}
    for(r=R-1;r>=e;r--)
    {
    
    
   		for(h=H-1;h>=e;h--)
		{
    
    
    	    c=2*r*h;
			v=r*r*h;
			if((C+((2*V)/r))>k&&k)
        	{
    
     
				continue;
			} 
    	    if(e==m)
    	    {
    
    
   				c+=r*r;
			}
        	DFS(e-1,V-v,C+c,r,h);
    	}
	}
}

Guess you like

Origin blog.csdn.net/weixin_46703995/article/details/107445197