HDU-1069 Monkey and Banana 动态规划

简单动态规划,将长方体分解成六个,然后二重排序后,DP即可。

对于当前方块,选择前面的能够放置的方块的最大值加上本身的高度。

代码如下:

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAXN 200
using namespace std;

// 对于每个长方体,有三种不同的方法。
int N, dp[MAXN];

struct Node
{
    int x, y, z;
}e[MAXN];

inline int max(int x, int y)
{
    return x > y ? x : y;
}

bool cmp(const Node &a, const Node &b)
{
    if (a.x != b.x)
        return a.x > b.x;
    else
        return a.y > b.y;
}

int judge(int a, int b)
{
    if (e[a].x > e[b].x && e[a].y > e[b].y) {
        return 1;
    }
    else {
        return 0;
    }
}

int DP()
{ 
    int Max = 0;
    memset(dp, 0, sizeof (dp)); 
    for (int i = 1; i <= 6*N; ++i) {
        dp[i] = e[i].z;
        Max = max(Max, dp[i]); // 没加这一句,一直WA
        for (int j = 1; j < i; ++j) {
            if (judge(j, i)) {
                dp[i] = max(dp[i], dp[j]+e[i].z);
                Max = max(Max, dp[i]); 
            }
        }
    }
    return Max;
}

int main()
{
    int ca = 1;
    while (scanf("%d", &N), N) {
        for (int i = 1; i <= 6*N; i+=6) {
            scanf("%d %d %d", &e[i].x, &e[i].y, &e[i].z);
            e[i+1].x = e[i].y, e[i+1].y = e[i].z, e[i+1].z = e[i].x;
            e[i+2].x = e[i].z, e[i+2].y = e[i].y, e[i+2].z = e[i].x;
            e[i+3].x = e[i].x, e[i+3].y = e[i].z, e[i+3].z = e[i].y;
            e[i+4].x = e[i].z, e[i+4].y = e[i].x, e[i+4].z = e[i].y;
            e[i+5].x = e[i].y, e[i+5].y = e[i].x, e[i+5].z = e[i].z;
        }
        sort(e+1, e+1+6*N, cmp); 
        printf("Case %d: maximum height = %d\n", ca++, DP());
    }
    return 0;
}
posted @ 2012-04-18 22:42  沐阳  阅读(261)  评论(0编辑  收藏  举报