首页 > 代码库 > Sharing Chocolate - UVa 1099 状压dp

Sharing Chocolate - UVa 1099 状压dp

Chocolate in its many forms is enjoyed by millions of people around the world every day. It is a truly universal candy available in virtually every country around the world.

You find that the only thing better than eating chocolate is to share it with friends. Unfortunately your friends are very picky and have different appetites: some would like more and others less of the chocolate that you offer them. You have found it increasingly difficult to determine whether their demands can be met. It is time to writte a program that solves the problem once and for all!


Your chocolate comes as a rectangular bar. The bar consists of same-sized rectangular pieces. To share the chocolate you may break one bar into two pieces along a division between rows or columns of the bar. You or the may then repeatedly break the resulting pieces in the same manner. Each of your friends insists on a getting a single rectangular portion of the chocolate that has a specified number of pieces. You are a little bit insistent as well: you will break up your bar only if all of it can be distributed to your friends, with none left over.


For exampla, Figure 9 shows one way that a chocolate bar consisting of x 4 pieces can be split into 4 parts that contain 6, 3, 2, and 1 pieces respectively, by breanking it 3 times (This corresponds to the first sample input.)

技术分享

Input 

The input consists of multiple test cases each describing a chocolate bar to share. Each description starts with a line containing a single integer n (1技术分享n技术分享15), the number of parts in which the bar is supposed to be split. This is followed by a line containing two integers x and y (1技术分享xy技术分享100), the dimensions of the chocolate bar. The next line contains n positive integers, giving the number of pieces that are supposed to be in each of the n parts.

The input is terminated by a line containing the integer zero.

Output 

For each test case, first display its case number. Then display whether it is possible to break the chocolate in the desired way: display ``Yes" if it is possible, and ``No" otherwise. Follow the format of the sample output.

Sample Input 

4 3 4 6 3 2 1 2 2 3 1 5 0

Sample Output 

Case 1: Yes Case 2: No

 

题意:问x*y的巧克力能否分成给定的n块大小。

思路:dp[c][S]表示状压状态为S的巧克力能否以c为短边组成矩形,然后dfs。

AC代码如下:

 

[cpp] view plaincopy技术分享技术分享
    1. #include<cstdio>  
    2. #include<cstring>  
    3. #include<algorithm>  
    4. using namespace std;  
    5. int sum[100010],num[20],vis[110][100010],dp[110][100010],pow2[20];  
    6. int n,t;  
    7. int dfs(int c,int S)  
    8. {  
    9.     if(vis[c][S]==t)  
    10.       return dp[c][S];  
    11.     int i,j,k,l,ans=0,S1,S2;  
    12.     vis[c][S]=t;  
    13.     for(j=0;j<n;j++)  
    14.        if(S==pow2[j])  
    15.           return dp[c][S]=1;  
    16.     l=sum[S]/c;  
    17.     for(S1=(S-1)&S;S1>0;S1=(S1-1)&S)  
    18.     {  
    19.         S2=S-S1;  
    20.         if(sum[S1]%c==0 && dfs(min(c,sum[S1]/c),S1) && dfs(min(c,sum[S2]/c),S2))  
    21.           return dp[c][S]=1;  
    22.         if(sum[S1]%l==0 && dfs(min(l,sum[S1]/l),S1) && dfs(min(l,sum[S2]/l),S2))  
    23.           return dp[c][S]=1;  
    24.     }  
    25.     return dp[c][S]=0;  
    26. }  
    27. int main()  
    28. {  
    29.     int i,j,k,c,l,S,ans;  
    30.     pow2[0]=1;  
    31.     for(i=1;i<=20;i++)  
    32.        pow2[i]=pow2[i-1]*2;  
    33.     while(~scanf("%d",&n) && n>0)  
    34.     {  
    35.         t++;  
    36.         scanf("%d%d",&c,&l);  
    37.         for(i=0;i<n;i++)  
    38.            scanf("%d",&num[i]);  
    39.         S=pow2[n]-1;  
    40.         for(i=1;i<=S;i++)  
    41.         {  
    42.             sum[i]=0;  
    43.             for(j=0;j<n;j++)  
    44.                if(i&pow2[j])  
    45.                  sum[i]+=num[j];  
    46.         }  
    47.         if(c*l!=sum[S])  
    48.           ans=0;  
    49.         else  
    50.           ans=dfs(min(c,l),S);  
    51.         printf("Case %d: ",t);  
    52.         if(ans)  
    53.           printf("Yes\n");  
    54.         else  
    55.           printf("No\n");  
    56.     }  

Sharing Chocolate - UVa 1099 状压dp