hdu3127 WHUgirls 残忍的完全双肩包。比较有意思,物品的那层循环放在最里面

hdu3127 WHUgirls 残忍的完全背包。。。比较有意思,,物品的那层循环放在最里面

WHUgirls

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2226    Accepted Submission(s): 844


Problem Description
There are many pretty girls in Wuhan University, and as we know, every girl loves pretty clothes, so do they. One day some of them got a huge rectangular cloth and they want to cut it into small rectangular pieces to make scarves. But different girls like different style, and they voted each style a price wrote down on a list. They have a machine which can cut one cloth into exactly two smaller rectangular pieces horizontally or vertically, and ask you to use this machine to cut the original huge cloth into pieces appeared in the list. Girls wish to get the highest profit from the small pieces after cutting, so you need to find out a best cutting strategy. You are free to make as many scarves of a given style as you wish, or none if desired. Of course, the girls do not require you to use all the cloth.
 

Input
The first line of input consists of an integer T, indicating the number of test cases.
The first line of each case consists of three integers N, X, Y, N indicating there are N kinds of rectangular that you can cut in and made to scarves; X, Y indicating the dimension of the original cloth. The next N lines, each line consists of two integers, xi, yi, ci, indicating the dimension and the price of the ith rectangular piece cloth you can cut in.
 

Output
Output the maximum sum of prices that you can get on a single line for each case.

Constrains
0 < T <= 20
0 <= N <= 10; 0 < X, Y <= 1000
0 < xi <= X; 0 < yi <= Y; 0 <= ci <= 1000
 

Sample Input
1 2 4 4 2 2 2 3 3 9
 

Sample Output
9
 上博客吧。http://blog.csdn.net/lulipeng_cpp/article/details/7587465
一开始是先循环的布块,怎么做都不对,后来终于发现,原来与顺序有关,想了想,理解如下:
因为放的方法,题目是要a machine which can cut one cloth into exactly two smaller rectangular pieces horizontally or vertically,大体意思就是,如果你要切的话,不是从里面抠出来,而是沿着边缘延伸,把它所在的那一块大的也切下来。因此,如果是先循环布块的话,是在整个布块中一块一块地切,那样的切法就是从里面抠。
而题目的切法,既然是顺着边沿,不妨把布块就放在右下角,然后扩展整个大的背景布块。小布块始终在右下角。(来源于discuss)。

代码:
#include <stdio.h>
#include <string.h>
#define MAX 1100

int dp[MAX][MAX] ;
int len[15] ,wid[15] , v[15];

int max(int a , int b)
{
	return a>b?a:b ;
}

int main()
{
	int t ;
	scanf("%d",&t);
	while(t--)
	{
		memset(dp,0,sizeof(dp)) ;
		int n , x , y;
		scanf("%d%d%d",&n,&x,&y) ;
		for(int i = 0 ; i < n ; ++i)
		{
			scanf("%d%d%d",&len[i],&wid[i],&v[i]) ;
		}
		
		for(int i = 0 ; i <= x ; ++i)
		{
			for(int j = 0 ; j <= y ; ++j)
			{
				for(int k = 0 ; k < n ; ++k)
				{
					if(i>=len[k] && j>=wid[k])
					{
						dp[i][j] = max(dp[i][j],max(dp[i][j-wid[k]]+dp[i-len[k]][wid[k]],dp[i-len[k]][j]+dp[len[k]][j-wid[k]])+v[k]) ;
					}
					if(i>=wid[k] && j>=len[k])
					{
						dp[i][j] = max(dp[i][j],max(dp[i-wid[k]][j]+dp[wid[k]][j-len[k]],dp[i][j-len[k]]+dp[i-wid[k]][len[k]])+v[k]) ;
					}
				}
			}
		}
		printf("%d\n",dp[x][y]) ;
	}
	return 0 ;
}


				与君共勉