题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3127
题目大意:一个大矩形,要你分割成小矩形(只能水平和竖直切割),每个小矩形都有一个价值,问如何分割能得到最大总价值。
解题思路:
完全背包思想,枚举所有可能情况。
对于枚举每个小矩形都可以横放竖放两种情况。
对于每种情况都有两种切割方式:横向切割或者竖向切割。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn=1024; 8 int dp[maxn][maxn]; 9 10 struct node 11 { 12 int x, y, val; 13 }f[maxn]; 14 15 int main() 16 { 17 int T, n, X, Y; 18 cin >> T; 19 while(T--) 20 { 21 cin >> n >>X >> Y; 22 for(int i=0; i<n; i++) 23 scanf("%d%d%d",&f[i].x,&f[i].y,&f[i].val); 24 memset(dp,0,sizeof(dp)); 25 for(int i=0; i<=X; i++) 26 for(int j=0; j<=Y; j++) 27 for(int k=0; k<n; k++) 28 { 29 if(i-f[k].x>=0&&j-f[k].y>=0) 30 dp[i][j]=max(dp[i][j],max(dp[i-f[k].x][j]+dp[f[k].x][j-f[k].y],dp[i-f[k].x][f[k].y]+dp[i][j-f[k].y])+f[k].val); 31 if(i-f[k].y>=0&&j-f[k].x>=0) 32 dp[i][j]=max(dp[i][j],max(dp[i-f[k].y][j]+dp[f[k].y][j-f[k].x],dp[i-f[k].y][f[k].x]+dp[i][j-f[k].x])+f[k].val); 33 } 34 cout << dp[X][Y] << endl; 35 } 36 return 0; 37 }