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
Sample Input
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上的动态规划这种理论高度(大佬就是强),有了这种主体思路,枚举起点,记忆化搜一发,很容易搞定。
#include <bits/stdc++.h> using namespace std; const int maxn = ; int n; int cube[maxn][];
int dp[maxn][]; void get_dimensions(int *v, int id, int dim) {
int idx = ;
for (int i = ; i < ; i++) {
if (i != dim) {
v[idx++] = cube[id][i];
}
}
} int DP(int i, int j) {
int& ans = dp[i][j];
if (ans > ) return ans; ans = ;
int v[], v2[];
get_dimensions(v, i, j);
for (int x = ; x <= n; x++) {
for (int y = ; y < ; y++) {
get_dimensions(v2, x, y);
if (v2[] < v[] && v2[] < v[]) {
ans = max(ans, DP(x, y));
}
}
}
ans += cube[i][j];
return ans;
} int T = ; int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n) && n) {
for (int i = ; i <= n; i++) {
for (int j = ; j < ; j++) {
scanf("%d", &cube[i][j]);
}
sort(cube[i], cube[i] + );
} memset(dp, -, sizeof(dp));
int ans = ;
for (int i = ; i <= n; i++) {
for (int j = ; j < ; j++) {
ans = max(ans, DP(i, j));
}
}
printf("Case %d: maximum height = %d\n", T++, ans);
}
return ;
}
UVA437-The Tower of Babylon(动态规划基础)的更多相关文章
- ACM - 动态规划 - UVA437 The Tower of Babylon
UVA437 The Tower of Babylon 题解 初始时给了 \(n\) 种长方体方块,每种有无限个,对于每一个方块,我们可以选择一面作为底.然后用这些方块尽可能高地堆叠成一个塔,要求只有 ...
- [动态规划]UVA437 - The Tower of Babylon
The Tower of Babylon Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many d ...
- Uva437 The Tower of Babylon
https://odzkskevi.qnssl.com/5e1fdf8cae5d11a8f572bae96d6095c0?v=1507521965 Perhaps you have heard of ...
- UVa437 The Tower of Babylon(巴比伦塔)
题目 有n(n<=30)种立方体,每种有无穷多个,摞成尽量高的柱子,要求上面的立方体要严格小于下面的立方体. 原题链接 分析 顶面的大小会影响后续的决策,但不能直接用d[a][b]来表示,因为可 ...
- 【DP】【Uva437】UVA437 The Tower of Babylon
传送门 Description Input Output Sample Input Sample Output Case : maximum height = Case : maximum heigh ...
- UVa 437 The Tower of Babylon(经典动态规划)
传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...
- DAG 动态规划 巴比伦塔 B - The Tower of Babylon
题目:The Tower of Babylon 这是一个DAG 模型,有两种常规解法 1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归 2.递推法 方法一:记忆化搜索 #include < ...
- UVa 437 The Tower of Babylon
Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...
- POJ 2241 The Tower of Babylon
The Tower of Babylon Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Or ...
随机推荐
- redis.conf 常见配置介绍
参数说明redis.conf 配置项说明如下: 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运 ...
- WarShall算法
1.引言 图的连通性问题是图论研究的重要问题之一,在实际中有着广泛的应用.例如在通信网络的联通问题中,运输路线的规划问题等等都涉及图的连通性.因此传递闭包的计算需要一个高效率的算法,一个著名的算法就是 ...
- 【转】Mybatis源码解读-设计模式总结
原文:http://www.crazyant.net/2022.html?jqbmtw=b90da1&gsjulo=kpzaa1 虽然我们都知道有26个设计模式,但是大多停留在概念层面,真实开 ...
- overall error
Overall error is same with total error in math.
- SpringMVC 与 REST.
一.REST 的基础知识 我敢打赌这并不是你第一次听到或读到REST这个词.当讨论REST时,有一种常见的错误就是将其视为“基于URL的Web服务”—— 将REST作为另一种类型的RPC机制,只不过是 ...
- JVM内存区域划分Eden Space,Survivor Space,Tenured Gen,Perm Gen
jvm区域总体分两类,heap区和非heap区.heap区又分:Eden Space(伊甸园).Survivor Space(幸存者区).Tenured Gen(老年代-养老区). 非heap区又分: ...
- IO学习二(节点流)
1.流的分类 按照数据流向的不同:输入流和输出流 按照处理数据的单位不同:字节流((非文本文件)视频.音频.图像).字符流(文本文件) 按照角色的不同:节点流和处理流 2.IO体系 抽象基类 节点流 ...
- MySQL 查看执行计划
MySQL 使用 explain + sql 语句查看 执行计划,该执行计划不一定完全正确但是可以参考. EXPLAIN SELECT * FROM user WHERE nid = 3; selec ...
- js实现获取当前时间是本月第几周和年的第几周的方法
js实现获取当前时间是本月第几周和年的第几周的方法 获取本月第几周的方法: var getMonthWeek = function (a, b, c) { /** * a = d = 当前日期 * b ...
- iOS开发GCD(3)-数据安全
/* 多个线程可能访问同一块资源,造成数据错乱和数据安全问题 为代码添加同步锁(互斥锁) */ -(void)synchronized{ @synchronized(self){ //需要锁住的代码, ...