如题:http://acm.hdu.edu.cn/showproblem.php?pid=3127
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.
Constrains
0 < T <= 20
0 <= N <= 10; 0 < X, Y <= 1000
0 < xi <= X; 0 < yi <= Y; 0 <= ci <= 1000
1 2 4 4 2 2 2 3 3 9
9
需要注意的是矩形的裁法。
题目要求是一刀切到底
因此:
dp[i][j]代表当前i*j矩形的最大价值
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define max(a,b)(a>b?a:b)
int N,X,Y;
int dp[1005][1005];
struct nod
{
int x,y,v;
};
nod node[12];
int cmp(nod &a,nod &b)
{
if(a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
}
int main()
{
//freopen("C:\\1.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&N,&X,&Y);
int i,j,k;
memset(dp,0,sizeof(dp));
memset(node,0,sizeof(node));
for(i=1;i<=N;i++)
{
int tx,ty,val;
scanf("%d%d%d",&tx,&ty,&val);
node[i].x=tx;
node[i].y=ty;
node[i].v=val;
}
int max=0;
sort(node+1,node+1+N,cmp);
for(i=0;i<=X;i++)
for(j=0;j<=Y;j++)
{
for(k=1;k<=N;k++)
{
int tx=node[k].x;
int ty=node[k].y;
if(i>=tx&&j>=ty)
dp[i][j]=max(dp[i][j],max(dp[tx][j-ty]+dp[i-tx][j]+node[k].v,dp[i-tx][ty]+dp[i][j-ty]+node[k].v));
if(i>=ty&&j>=tx)
dp[i][j]=max(dp[i][j],max(dp[ty][j-tx]+dp[i-ty][j]+node[k].v,dp[i-ty][tx]+dp[i][j-tx]+node[k].v));
}
}
printf("%d\n",dp[X][Y]);
}
return 0;
}