Description
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 example, Figure 9 shows one way that a chocolate bar consisting of 3 × 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 ≤ x, y ≤ 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块面积分别为ai的巧克力,每次只能横着切或者竖着切。
解法:
首先枚举所有的选择情况,将被选中的巧克力面积加入sum数组,然后对选择和较小的那条边进行dfs,假如只剩下一块巧克力了(s中只有一位为1),return true,假如sum不能整除x(不是某边为x的巧克力),return false,否则枚举子集,假如能够将被选中的巧克力以x或者y为边切成子集和去掉子集的剩余部分,则return true,假如上述条件均不满足,return false。最后根据结果输出答案。
代码来自于https://blog.csdn.net/accelerator_/article/details/19285265
不知道为什么WA了,让我再研究一下。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
using namespace std;
int n,x,y,t=0;
int a[17+5],sum[(1<<17)+5],dp[(1<<17)+5][100+5],vis[(1<<17)+5][100+5];
int bitcount(int x) {
if (x==0) return 0;
return bitcount(x>>1)+(x&1);
}
bool solve(int s,int x){
if(vis[s][x])return dp[s][x];
vis[s][x]=1;
if(bitcount(s)==1)return dp[s][x]=true;
if(sum[s]%x)return dp[s][x]=false;
int y=sum[s]/x;
for (int ss=(s-1)&s;ss>0;ss=(ss-1)&s){
int sss=(s^ss);
if (sum[ss]%x==0&&solve(ss,min(x,sum[ss]/x))&&solve(sss,min(x,sum[sss]/x))) return dp[s][x]=true;
if (sum[ss]%y==0&&solve(ss,min(y,sum[ss]/y))&&solve(sss,min(y,sum[sss]/y))) return dp[s][x]=true;
}
return dp[s][x]=false;
}
int main()
{
while(scanf("%d",&n)&&n!=0){
scanf("%d%d",&x,&y);
memset(vis,0,sizeof(vis));
memset(sum,0,sizeof(sum));
for(int i=0;i<n;i++)scanf("%d",&a[i]);
for(int s=0;s<(1<<n);s++){
for(int i=0;i<n;i++)if(s&(1<<i))sum[s]+=a[i];
}
if(sum[(1<<n)-1]!=x*y)printf("Case %d: No\n", ++t);
else printf("Case %d: %s\n", ++t,solve((1<<n)-1,min(x,y))?"Yes":"No");
}
return 0;
}