POJ2241——The Tower of Babylon
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2207 | Accepted: 1244 |
Description
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
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
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
每个箱子产生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的更多相关文章
- poj2241 The Tower of Babylon
The Tower of Babylon 题意:给你n种石头,长x,宽y,高z,每种石头数目无限,一块石头能放到另一块上的条件是:长和宽严格小于下面的石头.问叠起来的最大高度. /* 有些类似“叠箱子 ...
- UVa 437 The Tower of Babylon(经典动态规划)
传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...
- UVa 437 The Tower of Babylon
Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...
- UVA 437 十九 The Tower of Babylon
The Tower of Babylon Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- UVA437-The Tower of Babylon(动态规划基础)
Problem UVA437-The Tower of Babylon Accept: 3648 Submit: 12532Time Limit: 3000 mSec Problem Descrip ...
- DAG 动态规划 巴比伦塔 B - The Tower of Babylon
题目:The Tower of Babylon 这是一个DAG 模型,有两种常规解法 1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归 2.递推法 方法一:记忆化搜索 #include < ...
- HOJ 1438 The Tower of Babylon(线性DP)
The Tower of Babylon My Tags Cancel - Seperate tags with commas. Source : University of Ulm Internal ...
- uva The Tower of Babylon[LIS][dp]
转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...
- [动态规划]UVA437 - The Tower of Babylon
The Tower of Babylon Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many d ...
随机推荐
- [置顶] 【cocos2d-x入门实战】微信飞机大战之十二:分数的本地存储
转载请表明地址:http://blog.csdn.net/jackystudio/article/details/12036237 作为一个单机游戏,连分数存储的的功能都没有,让它怎么在单机游戏圈里混 ...
- 如何在编译内核时添加缺少的固件(随着intel wireless 5100 AGN的 iwlwifi 案例)
我不知道你在笔记本使用 Linux 在内核编译无线wifi 不能用.我的书"关联 Y450"一个足够的旧书,随着无线网卡: $ lspci | grep Wireless 06:0 ...
- iOS开发CoreAnimation解读之二——对CALayer的分析
iOS开发CoreAnimation解读之二——对CALayer的分析 一.UIView中的CALayer属性 1.Layer专门负责view的视图渲染 2.自定义view默认layer属性的类 二. ...
- [HeadFirst-HTMLCSS学习笔记][第八章扩大你的词汇量]
字体 font-family,可指定多个候选 body{ font-family:Verdana,Geneva,Arial,sans-serif; } font-size 字体大小 body{ fon ...
- php + apache + mysql
http://archive.apache.org/dist/httpd/binaries/win32/ Apache msi 下载地址
- PHP学习笔记二十五【类的继承】
<?php //定义父类 class Stu{ public $name; protected $age; protected $grade; private $address;//私有变量不会 ...
- Masonry的一些使用。
除了等距的有问题(懒得改了),其他用过挺正常的,我自己也是刚使用,有问题还请海涵. 地址:http://pan.baidu.com/s/1boyxu8Z
- java连接sqL2008 数据库实例
package com.lzw; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSe ...
- OpenSSl编译
1.下载openssl代码,下载地址:http://www.openssl.org/source/ ,如果使用winrar解压失败的话(提示不能创建符号链接),可以关闭UAC.2.下载安装Active ...
- VLC的相关文档以及javascript接口
参看下面链接:VLC相关文档