动态规划_DAG例题_The Tower of Babylon(UVA437)

【The Tower of Babylon(UVA437)】

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 tex2html_wrap_inline32 . 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 and Output】

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 tex2html_wrap_inline40 , tex2html_wrap_inline42 and tex2html_wrap_inline44 .

Input is terminated by a value of zero (0) for n.

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


【解析】

典型的第一种DAG模型,将二维化为三维即可。

  • flag(i,j,k,h)函数判断是否右边其中,i、k表示上下两个立方体的编号,j、h分别表示i立方体以编号j为高的状态、k立方体以编号h为高的状态。类似嵌套矩形的判断。需注意,题意说明,立方体的个数无限多个,所以不能添加 | if(i==k) return false; | 的判断。
  • 状态转移方程:dp[i][j]=max(dp[k][h])+a[i][j] | flag(i,j,k,h)==true

【代码】

#include
#include
using namespace std;

int n, a[32][4], d[32][4];

bool flag(int index, int k, int i, int j)
{
    int x[2][2];
    for(int x1=0,x2=0; x1<3;x1++)
        if(x1!=k) x[0][x2++] = a[index][x1];
    for(int x1=0,x2=0; x1<3;x1++)
        if(x1!=j) x[1][x2++] = a[i][x1];
    if((x[0][0]1][0] && x[0][1]1][1])||(x[0][0]1][1] && x[0][1]1][0]))
        return true;
    else return false;
}

int dp(int index, int k)
{
    int& ans = d[index][k];
    if(ans>0) return ans;
    ans = a[index][k];
    for(int i=0; ifor(int j=0; j<3; j++)
            if(flag(index, k, i, j)) ans = max(ans, dp(i, j) + a[index][k]);
    return ans;
}

int main()
{
    int CASE = 0;
    while(cin>>n && n)
    {
        int ans = 0;
        memset(a, 0, sizeof(n));
        memset(d, -1, sizeof(d));
        for(int i=0; ifor(int j=0; j<3; j++)
                cin>>a[i][j];
        for(int i=0; ifor(int j=0; j<3; j++)
                ans = max(ans, dp(i, j));
        cout<<"Case "<<++CASE<<": maximum height = "<return 0;
}

你可能感兴趣的:(动态规划,uva,动态规划)