UVA 1099 - Sharing Chocolate(记忆化搜索+状态压缩)

来源:互联网 发布:厦门公安局网络刻章 编辑:程序博客网 时间:2024/05/17 22:29

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.)

\epsfbox{p4794.eps}

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$ \le$n$ \le$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$ \le$xy$ \le$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块,问能否分。

思路:记忆化搜索,一开始一整块,每次分成2部分,相当于两个集合,如果两个集合最短边都和一整块最短边一样,就可以组成,那么子问题变成求两个集合能不能分。那么就可以定义状态dp[s][x],s代表集合,x代表短边,然后预处理出集合之和sum,那么另一边y就等于sum[s]/x要注意特判一开始块大小等不等于x,y。

代码:

 #include <stdio.h>#include <string.h>#include <algorithm>#define min(a,b) ((a)<(b)?(a):(b))using namespace std;const int N = 17;const int MAXN = (1<<N);const int XN = 105;int n, x, y, big[N], sum[MAXN];bool dp[MAXN][XN], vis[MAXN][XN];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 s0 = (s - 1)&s; s0 > 0; s0 = (s0 - 1)&s) {int s1 = (s^s0);if (sum[s0]%x==0 && solve(s0, min(x, sum[s0]/x)) && solve(s1, min(x, sum[s1]/x))) return dp[s][x] = true;if (sum[s0]%y==0 && solve(s0, min(y, sum[s0]/y)) && solve(s1, min(y, sum[s1]/y))) return dp[s][x] = true;}return dp[s][x] = false;}int main() {int cas = 0;while (~scanf("%d", &n) && n) {scanf("%d%d", &x, &y);memset(vis, 0, sizeof(vis));memset(sum, 0, sizeof(sum));for (int i = 0; i < n; i++)scanf("%d", &big[i]);for (int state = 0; state < (1<<n); state++) {for (int i = 0; i < n; i++)if (state&(1<<i))sum[state] += big[i];}if (sum[(1<<n)-1] != x * y) {printf("Case %d: No\n", ++cas); continue;}printf("Case %d: %s\n", ++cas, solve((1<<n) - 1, min(x, y))?"Yes":"No");} return 0;}


2 0
原创粉丝点击