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 ...
随机推荐
- hdoj 3400 三分
两次三分 #include <iostream> #include <cstdio> #include <cstring> #include <cmath&g ...
- The account is locked
SQL> select * from v$version where rownum=1; BANNER --------------------------------------------- ...
- linux服务器时间同步
date命令: date :查看当前时间,结果如下:Tue Mar 4 01:36:45 CST 2014 date -s 09:38:40 :设置当前时间,结果如下:Tue Mar 4 09:38: ...
- samba服务器加入域控主机所需要修改的配置文件
samba服务器加入域控主机,成为域成员,当用户访问samba服务器上的共享文件时,直接到域控主机上进行认证.samba服务器上不需要像先前一样创建系统用户,创建samba用户及密码. 1.安装环境( ...
- json返回数据拼接HTML
<div class="box-lists"> </div> $.ajax({ url: 'AjaxPage/AjaxHandler.ashx', typ ...
- .net程序员转战android第三篇---登录模块之静态登录
这一篇我将分2个部分记录登录界面,第一部分是静态登录, 这部分将如何从界面布局.控件使用.文件关系.数据验证.登陆实现等5小块记录. 第二部分是动态登录,这块会基于上面的4小块,在数据验证不是静态数据 ...
- iOS 之 cocoapods安装与使用
我们都知道第三方库,一般使用cocoapods管理,cocoapods在我们IOS开发中有着很大的作用. 好了,现在看下它的安装步骤: 1.打开终端,输入 sudo gem install cocoa ...
- C# 操作系统防火墙
很多时候,我们的程序是通过网络通信(如TCP或者UDP协议+端口),而将制作好的程序安装包给客户用时,发现会出现不能通信的现象(或者在这台电脑是可以的,却在另一台不可以),原因是防火墙阻止了,需要添加 ...
- 被sjy带刷题#1
笔记[问题描述]给定一个长度为m的序列a,下标编号为1~m.序列的每个元素都是1~n的整数.定义序列的代价为 你现在可以选择两个数x和y,并将序列a中所有的x改成y.x可以与y相等.请求出序列 ...
- hdu3998 Sequence(最大流,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Sequence Time Limit: 2000/1000 MS (Java/O ...