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 ...
随机推荐
- Android设备内存和SD卡操作工具类
package cc.c; import java.io.File; import java.util.List; import android.os.StatFs; import java.io.F ...
- 解开Android应用程序组件Activity的"singleTask"之谜
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6714543 在Android应用程序中,可以配 ...
- last与lastlog命令
lastlog 列出所有用户最后登录的时间和登录终端的地址,如果此用户从来没有登录,则显示:**Never logged in**last 列出用户所有的登录时间和登录终端的地址
- 前端--关于CSS文本
文本是网页中最重要的一种内容形式,文本几乎可以写在任何地方,块级元素中可以写行内元素中也可以写.文本都是由一个个字符组成的 ,在css布局中,每一个字符都有一个em框,通常font-size设置的大小 ...
- javascript 阻止多次点击造成的轮播混乱
function nextSlider(){ //使用b作为开关,只有动画完成后才能进行下一次运动 if(b){ //如果b为真,则马上设置b为false,如果startmove的回调没有重新设置b的 ...
- jquery 根据网站url给导航nav添加active效果
后台的同事因为把nav公用了,所以无法单页添加active,一下方法通过判断url的后缀给当前页添加active $(function(){ var _nava= $('.nav .nav-wrapp ...
- HttpURLConnection 下载代码
private int downloadFile(final String apkurl, final String apkname) { Log.e(LOGTAG, "downloadAp ...
- 本地计算机上的OracleOraDb11g_home2TNSListener服务启动又停止了。
电脑上装了oracle后启动很慢,然后我就不oracle服务设置成手动启动,没想到今天启动的时候居然报错 折腾了一上午,终于搞定, 在环境变量中把ORACLE_HOME 设置成D:\app\XL\pr ...
- MySQL 基础 之 语句执行顺序
FORM: 对FROM的左边的表和右边的表计算笛卡尔积.产生虚表VT1 ON: 对虚表VT1进行ON筛选,只有那些符合<join-condition>的行才会被记录在虚表VT2中. JOI ...
- 1、Servlet 2、ServletConfig 3、ServletContext 4、HttpUrlConnection
1.Servlet 2.ServletConfig 3.ServletContext 4.HttpUrlConnection 07. 五 / J2EE / 没有评论 一.第一个Servlet的编写 ...