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
题目大意
给你一块长X Y的布和n中剪法,每一种剪法可以得到一个价值c,问这块布通过这n种剪法可以得到的最大价值,布可以不用全部用光,每种剪法可以使用多次。注意,没剪一次要一刀到底。
输入第一行表示T组测试样例
第二行表示n种剪法,和布的长宽X和Y
接下来n行表示每一种剪法所需要的xi,yi和价值ci
思路
很容易想到用数组dp[X][Y]来表示我消费X和Y时得到的最大价值,考虑每一种剪法xi,yi有两种可能性:
①
先剪竖的一刀再剪横的一刀,得到三个矩形,分别是(X-xi)Y、(xi*yi)、xi(Y-yi),我们的目的就是计算这三个矩形的最大价值。
其中最小的矩形xi*yi的价值即为v[i];
②
同理情况①也可以得到三个矩形(Y-yi)X、xi*yi、xi(Y-yi)至此我们可以得到一个状态转移方程:
dp[j][z]=max(dp[j][z],max((dp[j-ren[i].xl][z]+dp[ren[i].xl][z-ren[i].yl]),(dp[j][z-ren[i].yl]+dp[j-ren[i].xl][ren[i].yl]))+ren[i].v);
但是,我们在剪xi*yi时我们可以有如下蓝色剪法:
也就是矩形是可以旋转的所以,所以我们得到第二个转态转移方程:
dp[j][z]=max(dp[j][z],max((dp[j-ren[i].yl][ren[i].xl]+dp[j][z-ren[i].xl]),(dp[j-ren[i].yl][z]+dp[ren[i].yl][z-ren[i].xl]))+ren[i].v);
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1000+5;
struct proc
{
int xl;
int yl;
int v;
}ren[21];
int dp[maxn][maxn],t,n,x,y;
int main()
{
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d %d %d",&n,&x,&y);
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&ren[i].xl,&ren[i].yl,&ren[i].v);
}
for(int j=0;j<=x;j++)
{
for(int z=0;z<=y;z++)
{
for(int i=1;i<=n;i++)
{
if(j>=ren[i].xl&&z>=ren[i].yl)
dp[j][z]=max(dp[j][z],max((dp[j-ren[i].xl][z]+dp[ren[i].xl][z-ren[i].yl]),(dp[j][z-ren[i].yl]+dp[j-ren[i].xl][ren[i].yl]))+ren[i].v);
if(j>=ren[i].yl&&z>=ren[i].xl)
dp[j][z]=max(dp[j][z],max((dp[j-ren[i].yl][ren[i].xl]+dp[j][z-ren[i].xl]),(dp[j-ren[i].yl][z]+dp[ren[i].yl][z-ren[i].xl]))+ren[i].v);
}
}
}
printf("%d\n",dp[x][y]);
}
return 0;
}