UVA437-The Tower of Babylon(动态规划基础)

Problem UVA437-The Tower of Babylon

Accept: 3648  Submit: 12532
Time Limit: 3000 mSec

Problem Description

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 thetwobasedimensionsoftheupperblockwerebothstrictlysmallerthanthecorresponding 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

题解:原来也做过这种题,但是从来没有升华到DAG上的动态规划这种理论高度(大佬就是强),有了这种主体思路,枚举起点,记忆化搜一发,很容易搞定。

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 const int maxn = 40;
 6
 7 int n;
 8
 9 int cube[maxn][3];
10 int dp[maxn][3];
11
12 void get_dimensions(int *v, int id, int dim) {
13     int idx = 0;
14     for (int i = 0; i < 3; i++) {
15         if (i != dim) {
16             v[idx++] = cube[id][i];
17         }
18     }
19 }
20
21 int DP(int i, int j) {
22     int& ans = dp[i][j];
23     if (ans > 0) return ans;
24
25     ans = 0;
26     int v[2], v2[2];
27     get_dimensions(v, i, j);
28     for (int x = 1; x <= n; x++) {
29         for (int y = 0; y < 3; y++) {
30             get_dimensions(v2, x, y);
31             if (v2[0] < v[0] && v2[1] < v[1]) {
32                 ans = max(ans, DP(x, y));
33             }
34         }
35     }
36     ans += cube[i][j];
37     return ans;
38 }
39
40 int T = 1;
41
42 int main()
43 {
44     //freopen("input.txt", "r", stdin);
45     while (~scanf("%d", &n) && n) {
46         for (int i = 1; i <= n; i++) {
47             for (int j = 0; j < 3; j++) {
48                 scanf("%d", &cube[i][j]);
49             }
50             sort(cube[i], cube[i] + 3);
51         }
52
53         memset(dp, -1, sizeof(dp));
54         int ans = 0;
55         for (int i = 1; i <= n; i++) {
56             for (int j = 0; j < 3; j++) {
57                 ans = max(ans, DP(i, j));
58             }
59         }
60         printf("Case %d: maximum height = %d\n", T++, ans);
61     }
62     return 0;
63 }

原文地址:https://www.cnblogs.com/npugen/p/9726988.html

时间: 2024-08-06 19:48:39

UVA437-The Tower of Babylon(动态规划基础)的相关文章

uva437 - The Tower of Babylon(DAG上的DP)

题目:uva437 - The Tower of Babylon(DAG上的DP) 题目大意:给你一些立方体,给出长宽高XYZ.现在希望你将这些立方题叠起来,使得最后的高度最大,并且这些立方体是可以无限次使用的,但是一个立方体要在另一个立方体的上面的话是需要满足这个立方体的底面是可以完全包含在下面的那个立方体的底面. 解题思路:其实这里的无限次使用没有什么用,因为一个立方体最多使用三次就不可能是再用.输入的一个立方体其实可以变成三个确定长宽高的立体.然后将这些立方体先做预处理,如果立方体j能够放

UVa 437 The Tower of Babylon(动态规划)

传送门 Description 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

uva 437 The Tower of Babylon(动态规划 嵌套矩形问题最长路)

有思路就去做,要相信自己 多处理更复杂的情况,你就不觉得现在复杂了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; struct soli { ll a,b,c; }s[40]; int n; ll d[40][3]; int vis[40][3]; ll answer[40][3]; ll dp(int s1,int s2

uva 动态规划 437 The Tower of Babylon

The Tower of Babylon Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in

UVa 437 The Tower of Babylon

Description 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 typ

UVA 437 The Tower of Babylon DP

有向图DAG The Tower of Babylon Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So n

POJ 2241 The Tower of Babylon

The Tower of Babylon Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 224164-bit integer IO format: %lld      Java class name: Main Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many d

UVA The Tower of Babylon

The Tower of Babylon 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

nyist oj 36 最长公共子序列 (动态规划基础题)

最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列. tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最长公共子序列. 输入 第一行给出一个整数N(0<N<100)表示待测数据组数 接