Uva437 The Tower of Babylon
Posted 嘒彼小星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Uva437 The Tower of Babylon相关的知识,希望对你有一定的参考价值。
https://odzkskevi.qnssl.com/5e1fdf8cae5d11a8f572bae96d6095c0?v=1507521965
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
【题解】
考虑a,b,c三个长度哪一个做高,分别用1/2/3表示
dp[i][1/2/3]表示i在最上面,a/b/c作高的最大高度
其实是一个DAG
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cmath> 7 #define max(a, b) ((a) > (b) ? (a) : (b)) 8 #define min(a, b) ((a) < (b) ? (a) : (b)) 9 inline void swap(int &a, int &b) 10 { 11 int tmp = a;a = b;b = tmp; 12 } 13 inline void read(int &x) 14 { 15 x = 0;char ch = getchar(), c = ch; 16 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 17 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 18 if(c == ‘-‘)x = -x; 19 } 20 21 const int MAXN = 40 + 5; 22 23 struct Node 24 { 25 int a,b,c; 26 int geta(int num) 27 { 28 if(num == 1)return a; 29 else if(num == 2)return b; 30 else return c; 31 } 32 int getb(int num) 33 { 34 if(num == 1)return b; 35 else if(num == 2)return c; 36 else return a; 37 } 38 int getc(int num) 39 { 40 if(num == 1)return c; 41 else if(num == 2)return a; 42 else return b; 43 } 44 }node[MAXN]; 45 46 int tt, dp[MAXN][MAXN], n; 47 48 int cmp(Node a, int numa, Node b, int numb) 49 { 50 return (a.geta(numa) < b.geta(numb) && a.getb(numa) < b.getb(numb)) || (a.geta(numa) < b.getb(numb) && a.getb(numa) < b.geta(numb)); 51 } 52 53 int f(int a, int b) 54 { 55 if(dp[a][b] != -1)return dp[a][b]; 56 dp[a][b] = node[a].getc(b); 57 for(register int i = 1;i <= n;++ i) 58 { 59 for(register int j = 1;j <= 3;++ j) 60 { 61 if(cmp(node[i],j,node[a],b)) 62 dp[a][b] = max(dp[a][b], f(i, j) + node[a].getc(b)); 63 } 64 } 65 return dp[a][b]; 66 } 67 68 int main() 69 { 70 while(scanf("%d", &n) != EOF && n) 71 { 72 ++ tt; 73 memset(dp, -1, sizeof(dp)); 74 for(register int i = 1;i <= n;++ i) 75 read(node[i].a), read(node[i].b), read(node[i].c); 76 int ans = -1; 77 for(register int i = 1;i <= n;++ i) 78 for(register int j = 1;j <= 3;++ j) 79 ans = max(ans, f(i, j)); 80 printf("Case %d: maximum height = %d\n", tt, ans); 81 } 82 return 0; 83 }
以上是关于Uva437 The Tower of Babylon的主要内容,如果未能解决你的问题,请参考以下文章