The Tower of Babylon
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2207   Accepted: 1244

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

Source

Ulm Local 1996



每个箱子产生6种状态,然后假设两个箱子能够叠放,就连一条边,最后记忆化搜索即可

#include<map>
#include<set>
#include<list>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; struct node
{
int x, y, z;
}block[200]; int cnt;
int dp[200];
bool g[200][200]; bool is_ok(node a, node b)
{
if(a.x < b.x && a.y < b.y)
{
return true;
}
return false;
} void get_block(int x, int y, int z)
{
block[cnt].x = x;
block[cnt].y = y;
block[cnt].z = z;
cnt++;
} int dfs(int i)
{
if(dp[i])
{
return dp[i];
}
dp[i] = block[i].z;
for(int j = 0; j < cnt; j++)
{
if(g[i][j])
{
dp[i] = max(dp[i], dfs(j) + block[i].z);
}
}
return dp[i];
} int main()
{
int n;
int icase = 1;
while (~scanf("%d", &n), n)
{
int x, y, z;
cnt = 0;
for(int i = 0; i < n; i++)
{
scanf("%d%d%d", &x, &y, &z);
get_block(x, y, z);
get_block(x, z, y);
get_block(y, x, z);
get_block(y, z, x);
get_block(z, x, y);
get_block(z, y, x);
}
memset( dp, 0, sizeof(dp) );
memset( g, 0, sizeof(g) );
for (int i = 0; i < cnt; i++)
{
for(int j = 0; j < cnt; j++)
{
if( is_ok(block[i], block[j]) )
{
g[i][j] = 1;
}
}
}
int ans = 0;
for(int i = 0; i < cnt; i++)
{
ans = max(ans, dfs(i));
}
printf("Case %d: maximum height = %d\n", icase++, ans);
}
return 0;
}

POJ2241——The Tower of Babylon的更多相关文章

  1. poj2241 The Tower of Babylon

    The Tower of Babylon 题意:给你n种石头,长x,宽y,高z,每种石头数目无限,一块石头能放到另一块上的条件是:长和宽严格小于下面的石头.问叠起来的最大高度. /* 有些类似“叠箱子 ...

  2. UVa 437 The Tower of Babylon(经典动态规划)

    传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...

  3. UVa 437 The Tower of Babylon

    Description   Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...

  4. UVA 437 十九 The Tower of Babylon

    The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

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

    Problem UVA437-The Tower of Babylon Accept: 3648  Submit: 12532Time Limit: 3000 mSec Problem Descrip ...

  6. DAG 动态规划 巴比伦塔 B - The Tower of Babylon

    题目:The Tower of Babylon 这是一个DAG 模型,有两种常规解法 1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归 2.递推法 方法一:记忆化搜索 #include < ...

  7. HOJ 1438 The Tower of Babylon(线性DP)

    The Tower of Babylon My Tags Cancel - Seperate tags with commas. Source : University of Ulm Internal ...

  8. uva The Tower of Babylon[LIS][dp]

    转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...

  9. [动态规划]UVA437 - The Tower of Babylon

     The Tower of Babylon  Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many d ...

随机推荐

  1. [think in java]知识点学习

    java中 全部数值都有正负号,不存在无符号整数. java中的基本类型存储在堆栈中. 其它对象存储在堆中. java确保数组会被初始化,并且不能在它的范围之外被訪问. 下面代码在c和c++中是合法的 ...

  2. IOS 怎么修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色

    self.navigationController.navigationBar.barTintColor = [UIColor blackColor]; self.navigationControll ...

  3. 设计模式之八:外观模式(Facade)

    外观模式: 为子系统中的一系列接口提供了一个统一的界面.外观模式定义了一个高层次的接口以使子系统更加easy使用. Provide a unified interface to a set of in ...

  4. EffectiveC#8--确保0对于值类型数据是有效的(初始化问题)

    1.决不要创建一个不包括0在内的枚举类型 2.举例如下: public enum Planet { Mercury = 1, Venus = 2, Earth = 3, Mars = 4, Jupit ...

  5. jquery模拟checkbox效果,以及background-size在jquery中的使用。

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  6. C#使用seleium实现一个自动登录器

    1.http://docs.seleniumhq.org/ 下载seleium包 2.新建一个C#项目,比如控制台,引用seleium包中的dll using System; using System ...

  7. hdu 1042 N!(高精度乘法)

    Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N in ...

  8. YUI的UA检测

    YUI.UA是针对javascript的宿主环境检测的一个检测对象,返回的是一系统关于当前宿主的信息 1.对象相关信息列表及userAgent 检测对象o = { ie: 0, //ie Mozill ...

  9. vs2003的代码考到vs2010 会出现(Windows CR LF)

    拷贝到VS中,出现各种莫名其妙的编译错误,这个时候需要注意,造成这种情况的原因可能在于,你所拷贝的源代码在换行时并非采用Windows系统的CR LF方式. 下面给出一个简单的方法来将不合要求的文本更 ...

  10. TreeSet与HashSet的区别

    Set是java中一个不包含重复元素的collection.更正式地说,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素.正如其名称所暗示的, ...