UVA437(DP)

题意:求所给的n种石头最高能组成多高的塔,其中组成塔的石头的两条边必须分别大于它上面的石头的两条边。每种石头有无限个并且可以随意翻转。

这道题容易迷惑人的一点是石头是无限的,但仔细考虑由于有大小的限制,所以同一个塔每种石头最多用2次,这样就相当于每种石头有两个了。显然,这是一道DAG上的动规,如果一个石头的两个边分别小于另一个石头的两条边,则说明这两个石头有边相连。由于石头可以随意翻转,所以一个石头的高度最多有3种,为了简便,把一个石头的3个翻转状态都当做一个石头来看待,那么在求解过程中就不用考虑翻转了,相当于总共有3n个顶点,把每个顶点当做一个状态来计算,由于起点和终点不明确,不适合使用递推,所以使用记忆化搜索。状态转移方程为:dp[i] = max{ dp[j]+h }, 其中i,j 有边相连。代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct Stone
{
    int l;
    int w;
    int h;
    Stone()
    {
    }
    Stone(int a, int b, int c)
    {
        l = a;
        w = b;
        h = c;
    }
};
Stone st[100];
int dp[100], n, k;

int solve(int i)
{
    if(dp[i] != 0)
        return dp[i];
    dp[i] = st[i].h;
    int j;
    for(j = 0; j <= k; j++)
    {
        if((st[j].l < st[i].l && st[j].w < st[i].w) ||
                (st[j].l < st[i].w && st[j].w < st[i].l))   //相当于有边相连
        {
            dp[i] = max(dp[i], solve(j) + st[i].h);
        }
    }
    return dp[i];
}
int main()
{
    int cas = 1;
    while(1)
    {
        scanf("%d", &n);
        if(n == 0)
            break;
        int i, x, y, z;
        k = 0;
        for(i = 0; i < n; i++)
        {
            scanf("%d%d%d", &x, &y, &z);
            st[k++] = Stone(x, y, z);       //石头的每种翻转情况都看做是另一种石头,计算时不考虑翻转。
            st[k++] = Stone(x, z, y);
            st[k++] = Stone(y, z, x);
        }
        memset(dp, 0, sizeof(dp));
        int ans = 0;
        for(i = 0; i < k; i++)
        {
            ans = max(ans, solve(i));       //找出最高高度
        }
        printf("Case %d: maximum height = %d\n", cas++, ans);
    }
    return 0;
}
另外还有一种使用递推解法,递推的关键是确定起点和终点。由于题目上要求组成塔的石头的两条边必须分别大于它上面的石头的两条边,约束条件是两个,不能按这个排序。但考虑到下面的面积一定大于上面的面积,所以可以按面积来进行排序,从面积小的向面积大的方向递推,最终找出所有状态中最大的即可。代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct Stone
{
    int l;
    int w;
    int h;

    Stone()
    {
    }
    Stone(int a, int b, int c)
    {
        l = a;
        w = b;
        h = c;
    }
};
Stone st[100];
int dp[100], n;

bool comp(Stone a, Stone b)
{
    return a.l * a.w < b.l * b.w;
}

int main()
{
    int cas = 1;
    while(1)
    {
        scanf("%d", &n);
        if(n == 0)
            break;
        int i, x, j, y, z, k = 0;
        for(i = 0; i < n; i++)
        {
            scanf("%d%d%d", &x, &y, &z);
            st[k++] = Stone(x, y, z);       //石头的每种翻转情况都看做是另一种石头,计算时不考虑翻转。
            st[k++] = Stone(x, z, y);
            st[k++] = Stone(y, z, x);
        }
        sort(st, st+k, comp);
        int ans = 0;
        for(i = 0; i < k; i++)
        {
            dp[i] = st[i].h;
            for(j = 0; j < i; j++)
            {
                if((st[j].l < st[i].l && st[j].w < st[i].w) ||
                   (st[j].l < st[i].w && st[j].w < st[i].l))
                {
                    dp[i] = max(dp[i], dp[j] + st[i].h);
                }
            }
            ans = max(ans, dp[i]);
        }
        printf("Case %d: maximum height = %d\n", cas++, ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值