hdu 3127 矩形分割 值得思考的完全背包

来源:互联网 发布:ubuntu kde gnome 编辑:程序博客网 时间:2024/06/12 19:41
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
 
题目大意是有一个大的矩形,让你分成小的矩形,现在给出小的矩形边长和价值,问可以分到的最大价值是多少。
本题死活想不出来怎么动态规划,后来看到别人的题解微微有点明白。现在总结一下:
动态规划最重要的是状态转移方程。状态转移方程怎么找呢,应该是模拟分了一块,然后看剩下的怎么表示。推导出方程。
本题方程就不再叙述,就是看那个小矩形横着放还是竖着放,横着有两种切法,竖着也有两种切法。找出最大的。
借用大神的图片:

只是有点不明白,这题竟然和for循环顺序有关。小的布放到外层循环就wrong,放到内层循环就Ac。不知道为什么。
摘抄一段杭电大神的解释:
一开始是先循环的布块,怎么做都不对,后来终于发现,原来与顺序有关,想了想,理解如下:因为放的方法,题目是要a machine which can cut one cloth into exactly two smaller rectangular pieces horizontally or vertically,大体意思就是,如果你要切的话,不是从里面抠出来,而是沿着边缘延伸,把它所在的那一块大的也切下来。因此,如果是先循环布块的话,是在整个布块中一块一块地切,那样的切法就是从里面抠。而题目的切法,既然是顺着边沿,不妨把布块就放在右下角,然后扩展整个大的背景布块。小布块始终在右下角。
可是我对这种解释有不一样的看法,我认为怎么切是状态转移方程决定的,状态转移方程那么写,就是表明咱是从边缘切算的,应该和for循环没有关系。

又找到一个大神的说法:
注意: 完全背包问题限制条件的维度j和物品编号的维度i的循环先后顺序是可以互换的. 但是此题必须先循环X和Y的维度, 然后才是物品编号的维度. 因为一般的完全背包问题的最优解对于物品的选取顺序没有要求, 可以先区任何物品. 但是此题对于原始矩形来说, 你在它的顶角边缘先切割那个小矩形是明显不同的,有可能你先切割1号矩形就得不到最优解, 但是你先切割3号矩形才能得到最优解(仔细想想是不是).
我认为这种说法是对的,有点类似于原来做的那个01背包,需要消除后效性,因为这题也有后效性。比如说当前切割一个最小的矩形,在当前是最优解。但是切另一个后还能切这个的话,而切了这个就不能切另一个。假如矩形都是d[i][j],这时切得方法不同,最优解就不同,所以这题有后效性。被切的布从最小开始,小矩形放到里面,每种情况都切一下,消除后效性。如果不这样,小矩形的for循环放到外面,假如先切1号得不到最优解,当你反过来切3号时候是依赖一号的值的,自然也得不到最优解。这种题一定要慎重考虑~~~
代码:

#include <stdio.h>#include <stdlib.h>#include <string.h>int dp[1001][1001];int row[11];int col[11];int val[11];int max(int a, int b){    return a > b ? a :b;}int main(){    int t, n, x, y;    int i, j, k;    scanf("%d", &t);    while (t--)    {        scanf("%d %d %d", &n, &x, &y);        for (i = 1; i <= n; i++)        {            scanf("%d %d %d", &row[i], &col[i], &val[i]);        }        memset(dp, 0, sizeof(dp));        for (j = 1; j <= x; j++)        {            for (k = 1; k <= y; k++)            {                for (i = 1; i <= n; i++)                {                    if (j - row[i] >= 0 && k - col[i] >= 0)                    {                        dp[j][k] = max(dp[j][k], max(dp[j - row[i]][k] + dp[row[i]][k - col[i]], dp[j - row[i]][col[i]] + dp[j][k - col[i]]) + val[i]);                    }                    if (j - col[i] >= 0 && k - row[i] >= 0)                    {                        dp[j][k] = max(dp[j][k], max(dp[j-col[i]][k] + dp[col[i]][k - row[i]], dp[j][k - row[i]] + dp[j - col[i]][row[i]]) + val[i]);                    }                }            }        }        printf("%d\n", dp[x][y]);    }    return 0;}

错误代码也粘贴一下,以后方便查看:

#include <stdio.h>#include <stdlib.h>#include <string.h>int dp[1001][1001];int row[11];int col[11];int val[11];int max(int a, int b){    return a > b ? a :b;}int main(){    int t, n, x, y;    int i, j, k;    scanf("%d", &t);    while (t--)    {        scanf("%d %d %d", &n, &x, &y);        for (i = 1; i <= n; i++)        {            scanf("%d %d %d", &row[i], &col[i], &val[i]);        }        memset(dp, 0, sizeof(dp));        for (i = 1; i <= n; i++)        {            for (j = 1; j <= x; j++)            {                for (k = 1; k <= y; k++)                {                    if (j - row[i] >= 0 && k - col[i] >= 0)                    {                        dp[j][k] = max(dp[j][k], max(dp[j - row[i]][k] + dp[row[i]][k - col[i]], dp[j - row[i]][col[i]] + dp[j][k - col[i]]) + val[i]);                    }                    if (j - col[i] >= 0 && k - row[i] >= 0)                    {                        dp[j][k] = max(dp[j][k], max(dp[j-col[i]][k] + dp[col[i]][k - row[i]], dp[j][k - row[i]] + dp[j - col[i]][row[i]]) + val[i]);                    }                }            }        }        printf("%d\n", dp[x][y]);    }    return 0;}






0 0
原创粉丝点击