题意:有n种立方体,每种都有无穷多个。选一些正方体摞成一根尽量高的柱子(可以选择任意一条边做高),使得每个立方体的底面长宽分别严格小于它下方的立方柱的底面长宽。

题解:可以套用DAG最长路算法,可以使用二元组来表示每个立方体的每一条边,如v[n][2]就可以用来表示第n个立方块的3个边。

DAG最长路算法:

int dp(int i,int j)
{
int &ans=dist[i][j];
if(ans>) return ans;///表示已经查找过此种状态
ans=;///根据题意赋相应的初值
int v[],v2[];
get_dimensions(v,i,j);///用v数组表示每个方块的长宽高
for(int a=;a<n;a++)
for(int b=;b<;b++){///对每种放置方法进行枚举
get_dimensions(v2,a,b);
if(v2[]<v[]&&v2[]<v[]) ans=max(ans,dp(a,b));///符合条件的方块,进行放置,查看是否最优,此处只是对下一个状态进行最优查找,并未将当前状态的高度计入(因为当前方案不一定可行)
}
ans+=blocks[i][j];///放置此种方块的方案可行,进行放置
return ans;
}

本题代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=+;
int blocks[maxn][],dist[maxn][],n; void get_dimensions(int* v, int b, int dim) {
int idx = ;
for(int i=;i<;i++)
if(i != dim) v[idx++] = blocks[b][i];///找出每个方块对应的长,宽,高
} int dp(int i,int j)
{
int &ans=dist[i][j];
if(ans>) return ans;///表示已经查找过此种状态
ans=;
int v[],v2[];
get_dimensions(v,i,j);///用v数组表示每个方块的长宽高
for(int a=;a<n;a++)
for(int b=;b<;b++){///对每种放置方法进行枚举
get_dimensions(v2,a,b);
if(v2[]<v[]&&v2[]<v[]) ans=max(ans,dp(a,b));///符合条件的方块,进行放置,查看是否最优,此处只是对下一个状态进行最优查找,并未将当前状态的高度计入(因为当前方案不一定可行)
}
ans+=blocks[i][j];///放置此种方块的方案可行,进行放置
return ans;
} int main()
{
int kase=;
while(~scanf("%d",&n)&&n){
for(int i=;i<n;i++){
for(int j=;j<;j++) scanf("%d",&blocks[i][j]);
sort(blocks[i],blocks[i]+);
}
memset(dist,,sizeof(dist));
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",++kase,ans);
}
return ;
}

The Tower of Babylon(UVa 437)的更多相关文章

  1. The Tower of Babylon UVA - 437 DAG上的动态规划

    题目:题目链接 思路:每个方块可以用任意多次,但因为底面限制,每个方块每个放置方式选一个就够了,以x y为底 z 为高,以x z为底 y 为高,以y z为底 x为高,因为数据量很小,完全可以把每一种当 ...

  2. UVA 437 十九 The Tower of Babylon

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

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

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

  4. UVa 437 The Tower of Babylon

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

  5. UVa 437 The Tower of Babylon(DP 最长条件子序列)

     题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于还有一个时  这个长方体能够放在还有一个上面  要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法  比較不好控制 ...

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

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

  7. UVA The Tower of Babylon

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

  8. POJ2241——The Tower of Babylon

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

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

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

随机推荐

  1. spring boot2+jpa+thymeleaf增删改查例子

    参考这遍文章做了一个例子,稍微不同之处,原文是spring boot.mysql,这里改成了spring boot 2.Oracle. 一.pom.xml引入相关模块web.jpa.thymeleaf ...

  2. SAP Hybris电子商务最新功能

    SAP Hybris电子商务最新功能   SAP Hybris 电子商务6.0中国加速器是专为中国市场设计的电子商务平台,可满足企业在全渠道销售和订单履行方面的所有需求.新版的中国加速器基于SAP H ...

  3. 5_bash

    bash及其特性:shell:外壳.用户直接接入计算机的时候所使用的外壳程序linux允许一个用户账户登录多次,而这多次登录的每一个打开的shell都是独立的互不相干的shell,它们是三个进程,每一 ...

  4. maven+springmvc出现:java.sql.SQLException: Unknown system variable 'query_cache_size'

    连接mysql时一直出现以下的错误: org.springframework.web.util.NestedServletException: Request processing failed; n ...

  5. css js 兼容问题

    js  兼容问题 1. document.form.item 问题问题:代码中存在 document.formName.item("itemName") 这样的语句,不能在FF下运 ...

  6. Spring Boot配置加载顺序

    如果加载的配置有重复的,它们的加载顺序是这样的,数字越小的优先级越高,即优先级高的覆盖优先级低的配置. Devtools global settings properties on your home ...

  7. Dubbox:来自当当网的SOA服务框架

    Dubbox:来自当当网的SOA服务框架 http://www.open-open.com/lib/view/open1417426480618.html

  8. jvm是如何管理内存的 .ZT

    http://blog.csdn.net/u014421556/article/details/51744044

  9. pc远程控制凭证不工作的解决办法

    感谢这位博主写的文章https://blog.csdn.net/u010433704/article/details/50679874 前提是要控制的电脑设置了密码,之前想不明白为什么需要这一项,后来 ...

  10. ireport使用

    首先需要下载ireport,可到https://zh.osdn.net/projects/sfnet_ireport/releases/下载,可能打开速度有点慢,耐心等待下,里面有各个版本,可自行选择 ...