The Tower of Babylon UVA - 437(巴比伦塔,DP)

Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale
have been forgotten. So now, in line with the educational nature of this contest, we will tell you the
whole story:
The babylonians had n types of blocks, and an unlimited supply of blocks of each type.
Each type-i block was a rectangular solid with linear dimensions (xi
, yi
, zi). A block could
be reoriented so that any two of its three dimensions determined the dimensions of the base
and the other dimension was the height.
They wanted to construct the tallest tower possible by stacking blocks. The problem was
that, in building a tower, one block could only be placed on top of another block as long as
the two base dimensions of the upper block were both strictly smaller than the corresponding
base dimensions of the lower block. This meant, for example, that blocks oriented to have
equal-sized bases couldn’t be stacked.
Your job is to write a program that determines the height of the tallest tower the babylonians can
build with a given set of blocks.
Input
The input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi
, yi and zi
.
Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line containing the case number (they are numbered sequentially starting
from 1) and the height of the tallest possible tower in the format
‘Case case: maximum height = height’
Sample Input
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
Sample Output
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342

题意: 堆木块,上一块底面的长宽必须小于下一个块的长宽,问最多堆多高。
思路: 很经典的题了,现在写。湖南多校遇到过一道类似的,但是是二维模型,那道题转换用了建图的方法,H - Highest Tower CSU - 2017 ,应该也能用dp的方法做出来,改天再补一补。

  • 状态定义:dp[i][j]代表选第i个木块,选第j条边为高(因为边长可能很多,所以用边长表示状态可能开不下)
  • 子状态:所有长宽小于这个木块的状态
  • 状态转移:取子状态max
  • 实现细节:使用了记忆化搜索表示状态转移的过程。使用了ans来表示dp数组,简化代码量。要记得ans选完子状态的max还得加上当前高度。
  • 为什么:关键就在于状态表示部分,这部分怎么想出来的?注意到一点,我们堆木块的时候只考虑底面,底面更小就可以堆上去,而高度是决策的结果,所以我们只取底面作为状态,取最优的结果就是最高的高度。
#include
#include
#include
#include
#include

using namespace std;
const int maxn = 105;

int n;
int dp[maxn][3];//选第i个木块且第j个边作为高的时候所得最高高度
int cube[maxn][3];

void trans(int *v,int a,int b)
{
    int cnt = 0;
    for(int i = 1;i <= 3;i++)
    {
        if(i == b)continue;
        v[cnt++] = cube[a][i];
    }
}
int solve(int a,int b)
{
    int &ans = dp[a][b];//用ans来代替dp数组,简化过程
    if(ans > 0)
        return ans;
    ans = 0;
    
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= 3;j++)
        {
            int v1[2],v2[2];
            trans(v1,i,j);
            trans(v2,a,b);
            if(v1[0] < v2[0] && v1[1] < v2[1])
                ans = max(ans,solve(i,j));
        }
    }
    ans += cube[a][b];
    return ans;
}

int main()
{
    
    int kase = 0;
    while(~scanf("%d",&n) && n)
    {
        memset(dp,0,sizeof(dp));
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= 3;j ++)
            {
                scanf("%d",&cube[i][j]);
            }
            sort(cube[i] + 1,cube[i] + 1 + 3);
        }
        
        int ans = 0;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= 3;j++)
            {
                ans = max(ans,solve(i,j));
            }
        }
        printf("Case %d: maximum height = ",++kase);
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(#,线性dp)