HDOJ 1069 Monkey and Banana(DP)

本文介绍了一种解决三维装箱问题的方法,通过对每个长方体进行不同的摆放方式转换为三个独立的长方体,进而将原问题转化为经典的01背包问题。通过动态规划算法求解最大高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

this is a simple 01 bags problem which need a little change of the number of goods. in this problem (link) we should stack our cuboid to the height given by problem,but we can’t determine the way of place the cuboid in equation . So we make one cuboid become three cuboids - the different ways of place them. Than we can solve this like normal 01 bags problem.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 50000;
struct node
{
    int x,y,h;
}block[510];
int dp[510];
int a[510];
int n = 0,len = 0;;
void place(int x,int y,int z)
{
    len++;
    block[len].x =x;
    block[len].y = y;
    block[len].h = z;
}
int cmp (node a,node b)
{
    return a.x > b.x;
}
int main()
{
    int cat = 1;
    while(cin>>n)
    {
        if(!n) break;
        len = 0;
        int ans = 0;
        int x,y,z;
        for(int i = 1;i<=n;i++)
        {
            cin>>x>>y>>z;

            place(x,y,z);
            place(x,z,y);
            place(y,x,z);
            place(y,z,x);
            place(z,y,x);
            place(z,x,y);
        }
        sort(block+1,block+len+1,cmp);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=len;i++)
        {
            dp[i] = block[i].h;
            for(int j = 1;j<i;j++)
            {
                if(block[i].x < block[j].x && block[i].y < block[j].y)
                {
                    dp[i] = max(dp[i],dp[j] + block[i].h);
                    ans = max(ans,dp[i]);
                }
            }
        }
        cout<<"Case "<<cat++<<": maximum height = "<<ans<<endl;

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值