hdu3127 WHUgirls 残忍的完全背包。。。比较有意思,,物品的那层循环放在最里面
2015-03-24 17:12
190 查看
WHUgirls
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2226 Accepted Submission(s): 844Problem DescriptionThere 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. InputThe 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. OutputOutput the maximum sum of prices that you can get on a single line for each case.Constrains0 < T <= 200 <= N <= 10; 0 < X, Y <= 10000 < xi <= X; 0 < yi <= Y; 0 <= ci <= 1000 Sample Input1 2 4 4 2 2 2 3 3 9Sample 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 1100int 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 ;}
与君共勉
相关文章推荐
- 完全背包基础详解(附求解选择的物品)
- uvaoj 10465 - Homer Simpson 物品无限的完全背包
- ACM:动态规划,物品无限的背包问题(完全背包问题)
- php里面include语句,不能放在循环里面,否则会出错,还不显示错误原因
- 背包问题——“完全背包”详解及实现(包含背包具体物品的求解)
- 关于01背包和完全背包二重循环的顺序(前人之技,后人惊叹)
- 背包问题——“完全背包”详解及实现 (包含背包具体物品的求解)
- ★★★★★背包问题——“01背包”"完全背包"详解及实现(包含背包中具体物品的求解)
- 背包系列第五篇----完全背包(求解最大价值时背包的物品)
- 背包问题——“完全背包”详解及实现(包含背包具体物品的求解)
- 判断一个窗体是否被完全遮挡(比较有意思,但这招有什么用呢?)
- POJ1787 【完全背包+物品计数+路径输出】
- wait必须放在while循环里面的原因探析
- soj 3531 Number Pyramids(观察组合数 + 每种物品至少选一个的完全背包)
- 背包问题----完全背包(详解|代码实现|背包具体物品的求解)
- 背包问题----完全背包(详解|代码实现|背包具体物品的求解)
- NGUI之设置背包系统里面的物品的可拖拽功能
- “完全背包”详解及实现(包含背包具体物品的求解)
- (hdu step 3.3.1)Big Event in HDU(01背包:N件物品放在容量为V的背包中,第i件物品的费用是c[i],价值是w[i]。问所能获取的最大价值)
- hdu 1059 有限物品完全背包