首页 > 代码库 > 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 3 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 (1n15), 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 (1x, y100), 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代码如下:
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- int sum[100010],num[20],vis[110][100010],dp[110][100010],pow2[20];
- int n,t;
- int dfs(int c,int S)
- {
- if(vis[c][S]==t)
- return dp[c][S];
- int i,j,k,l,ans=0,S1,S2;
- vis[c][S]=t;
- for(j=0;j<n;j++)
- if(S==pow2[j])
- return dp[c][S]=1;
- l=sum[S]/c;
- for(S1=(S-1)&S;S1>0;S1=(S1-1)&S)
- {
- S2=S-S1;
- if(sum[S1]%c==0 && dfs(min(c,sum[S1]/c),S1) && dfs(min(c,sum[S2]/c),S2))
- return dp[c][S]=1;
- if(sum[S1]%l==0 && dfs(min(l,sum[S1]/l),S1) && dfs(min(l,sum[S2]/l),S2))
- return dp[c][S]=1;
- }
- return dp[c][S]=0;
- }
- int main()
- {
- int i,j,k,c,l,S,ans;
- pow2[0]=1;
- for(i=1;i<=20;i++)
- pow2[i]=pow2[i-1]*2;
- while(~scanf("%d",&n) && n>0)
- {
- t++;
- scanf("%d%d",&c,&l);
- for(i=0;i<n;i++)
- scanf("%d",&num[i]);
- S=pow2[n]-1;
- for(i=1;i<=S;i++)
- {
- sum[i]=0;
- for(j=0;j<n;j++)
- if(i&pow2[j])
- sum[i]+=num[j];
- }
- if(c*l!=sum[S])
- ans=0;
- else
- ans=dfs(min(c,l),S);
- printf("Case %d: ",t);
- if(ans)
- printf("Yes\n");
- else
- printf("No\n");
- }
- }
Sharing Chocolate - UVa 1099 状压dp