hdu 3127 WHUgirls【dp】好题~

WHUgirls

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2796    Accepted Submission(s): 1046


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
 

Sample Input
  
  
1 2 4 4 2 2 2 3 3 9
 

Sample Output
  
  
9

题目大意:

一共有N种矩形纸片是有价值的,我们现在有一个X*Y的一个大纸片,让我们任意方式拆剪,问最大收益。


思路(好题啊,思路来源自:http://www.cnblogs.com/feature/articles/1856555.html):


1、分析问题,我们不难发现这是一个完全背包的模型。但是他肯定是二维的,那么我们设定dp【i】【j】表示i*j大小的纸片最大收益是多少。


2、那么接下来考虑有几种拆剪方式:



dp【i】【j】=max(dp【i】【j】,dp【i-x】【j】+dp【x】【j-y】+val);


dp【i】【j】=max(dp【i】【j】,dp【i-x】【y】+dp【i】【j-y】+val);

我们接下来还可以考虑将x,y竖过来来进行拆剪,所以一共是四种方式。


3、考虑到图形顺序之间可能会相互影响,所以我们枚举小纸片的时候,要在最内层的循环中进行。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,val;
}a[15];
int dp[1005][1005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        int n,row,col;
        scanf("%d%d%d",&n,&row,&col);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].val);
        }
        for(int i=1;i<=row;i++)
        {
            for(int j=1;j<=col;j++)
            {
                for(int z=0;z<n;z++)
                {
                    int x=a[z].x;
                    int y=a[z].y;
                    if(i>=x&&j>=y)
                    {
                        dp[i][j]=max(dp[i][j],dp[i-x][j]+dp[x][j-y]+a[z].val);
                        dp[i][j]=max(dp[i][j],dp[i][j-y]+dp[i-x][y]+a[z].val);
                    }
                    swap(x,y);
                    if(i>=x&&j>=y)
                    {
                        dp[i][j]=max(dp[i][j],dp[i-x][j]+dp[x][j-y]+a[z].val);
                        dp[i][j]=max(dp[i][j],dp[i][j-y]+dp[i-x][y]+a[z].val);
                    }
                }
            }
        }
        printf("%d\n",dp[row][col]);
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值