码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-29 23:48:42      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:BMI   ons   org   cat   ini   base   stdin   art   contain   

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 }

 

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

标签:BMI   ons   org   cat   ini   base   stdin   art   contain   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!