您的位置:首页 > 其它

HDU 1069 Monkey and Banana(dp)

2014-03-29 18:35 316 查看
题目链接:HDU 1069 Monkey and Banana

dp。

我认为这道题难点在于解决方块无限个的问题,转化到代码中就是一个方块变成六个方块。然后排序,剩下就简单了。

blocks[i].height表示第i个块在最底下时候的最大高度。

具体看代码吧。

#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int MAX_N = 1000;
struct Block
{
    int x,y,z;
    int height;
};
Block blocks[MAX_N];
int n,x,y,z,k,_max;
int cmp(Block a, Block b)
{
    if(a.x < b.x)
        return 1;
    if(a.x == b.x && a.y < b.y)
        return 1;
    return 0;
}
void f(int x, int y, int z)
{
    blocks[k].x = x;
    blocks[k].y = y;
    blocks[k].height = blocks[k].z = z;
    k++;
    blocks[k].x = x;
    blocks[k].y = z;
    blocks[k].height = blocks[k].z = y;
    k++;
    blocks[k].x = y;
    blocks[k].y = x;
    blocks[k].height = blocks[k].z = z;
    k++;
    blocks[k].x = y;
    blocks[k].y = z;
    blocks[k].height = blocks[k].z = x;
    k++;
    blocks[k].x = z;
    blocks[k].y = x;
    blocks[k].height = blocks[k].z = y;
    k++;
    blocks[k].x = z;
    blocks[k].y = y;
    blocks[k].height = blocks[k].z = x;
    k++;
}
int main()
{
    int cnt = 0;
    while(cin >> n, n)
    {
        _max = 0;
        k = 0;
        for(int i = 0;i < n;i++)
        {
            cin >> x >> y >> z;
            f(x, y, z);
        }
        sort(blocks, blocks + k, cmp);
        for(int i = 0;i < k;i++)
        {
            for(int j = 0 ;j < i;j++)
            {
                if(blocks[i].x > blocks[j].x && blocks[i].y > blocks[j].y)
                    blocks[i].height = max(blocks[i].height, blocks[j].height + blocks[i].z);
            }
            _max = max(_max, blocks[i].height);
        }
        //cout << _max << endl;
        printf("Case %d: maximum height = %d\n",++cnt,_max);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: