UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)
描述
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=378
n种方块,给出每一种的长宽高,现在要落起来,上面的方块的长和宽要严格小于下面的方块,问最多落多高.
ACM Contest Problems Archive
University of Valladolid (SPAIN)
437 The Tower of Babylon
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 ( x ; y ; z ). 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.
i
i
i
Input and Output
The input le will contain one or more test cases. The rst line of each test case contains an integer n ,
representing the number of dierent 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 x , y and z .
Input is terminated by a value of zero (0) for n .
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 "
i
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
i
iACM Contest Problems Archive
Sample Output
Case
Case
Case
Case
1:
2:
3:
4:
maximum
maximum
maximum
maximum
height
height
height
height
=
=
=
=
40
21
28
342
分析
直观的想法就是暴搜,但是有重叠自问题,所以可以做记忆化.但是用长度来做数组下标不切实际,所以有(idx,k)这个二元组表示第idx个方块,以第k条边作为高的情况,然后记忆化搜索即可.
注意:
1.对于一个点,搜索完了之后再加上他自己的高度.
#include <bits/stdc++.h>
using namespace std; const int maxn=+;
int n,kase;
int a[maxn][],dp[maxn][]; int dfs(int idx,int k){
int &ans=dp[idx][k];
if(ans) return ans;
int x1=a[idx][(k+)%],y1=a[idx][(k+)%];
for(int i=;i<=n;i++)
for(int j=;j<;j++){
int x2=a[i][(j+)%],y2=a[i][(j+)%];
if(x2<x1&&y2<y1||x2<y1&&y2<x1) ans=max(ans,dfs(i,j));
}
ans+=a[idx][k];
return ans;
}
void init(){
memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
for(int j=;j<;j++)
scanf("%d",&a[i][j]);
}
int main(){
while(scanf("%d",&n)&&n){
init();
int ans=;
for(int i=;i<=n;i++)
for(int j=;j<;j++)
ans=max(ans,dfs(i,j));
printf("Case %d: maximum height = %d\n",++kase,ans);
}
return ;
}
UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)的更多相关文章
- sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)
Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...
- Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)
描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提 ...
- Poj-P1088题解【动态规划/记忆化搜索】
本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=1088 题目描述: 区域由一个二维数组给 ...
- [NOIP2017] 逛公园 (最短路,动态规划&记忆化搜索)
题目链接 Solution 我只会60分暴力... 正解是 DP. 状态定义: \(f[i][j]\) 代表 \(1\) 到 \(i\) 比最短路长 \(j\) 的方案数. 那么很显然最后答案也就是 ...
- 滑雪---poj1088(动态规划+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 有两种方法 一是按数值大小进行排序,然后按从小到大进行dp即可: #include <iostream> #incl ...
- 由DAG到背包问题——记忆化搜索和递推两种解法
一.问题描述 物品无限的背包问题:有n种物品,每种均有无穷多个.第 i 种物品的体积为Vi,重量为Wi.选一些物品装到一个容量为 C 的背包中,求使得背包内物品总体积不超过C的前提下重量的最大值.1≤ ...
- 专题1:记忆化搜索/DAG问题/基础动态规划
A OpenJ_Bailian 1088 滑雪 B OpenJ_Bailian 1579 Function Run Fun C HDU 1078 FatMouse and Chee ...
- LightOJ1417 Forwarding Emails(强连通分量+缩点+记忆化搜索)
题目大概是,每个人收到信息后会把信息发给他认识的一个人如此下去,问一开始要把信息发送给谁这样看到信息的人数最多. 首先找出图中的SCC并记录每个SCC里面的点数,如果传到一个SCC,那么里面的人都可以 ...
- poj1351Number of Locks(记忆化搜索)
题目链接: 传送门 思路: 这道题是维基百科上面的记忆化搜索的例题... 四维状态dp[maxn][5][2][5]分别表示第几根棒子,这根棒子的高度,是否达到题目的要求和使用不同棒子数.那么接下来就 ...
随机推荐
- C#常用正则验证
#region Protected Property protected Regex rLetters { get { return new Regex("[a-zA-Z]{1,}" ...
- 升级Capitan 10.11以后CocoaPod 无效解决办法
今天发现升级10.11的系统以后执行 pod install 的时候报错 zsh: command not found: pod 解决方法如下: 1.检查gem 的数据源 gem sources -l ...
- Http上传文件
public class UpLoadFile { public static void UpLoadFiles(string fileName) { string fileType = Path.G ...
- JavaScript学习笔记 -- ES6学习(三) 变量的解构赋值
1.解构赋值的定义 在ES6中,允许按照一定模式,从数组和对象中提取值(所谓解构),然后对变量进行赋值. var a = 1; var b = 2; var c = 3; //等价于 var [a, ...
- 记一次ftp服务器错误 centOS 6.4 vsftpd 500 illegal port command
这个错误是因为是主动模式的,应该改为被动模式 以下是操作过程: iptables中加 -A INPUT -p tcp -m state --state NEW -m tcp --dport 10221 ...
- vim 自動化配置
Vim是Linux系統上常用的編輯器/Text Editor.不過很多人由於不瞭解如何配置,增加了很多煩惱. 今天介紹一個自動化的配置spf13,直接下載製作好的配置並進行自動設置. 1.官方的安裝步 ...
- Poj 3061 Subsequence(二分+前缀和)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...
- Core模块其他常用知识点[OpenCV 笔记14]
Matx 轻量级的Mat,必须在使用前规定好大小,比如一个2x3的float型的Matx,可以声明为Matx23f Vec Vec是Matx的一个派生类,是一个一维的Matx,跟vector很相似.在 ...
- mkpasswd
-l # (length of password, default = 7) 指定密码的长度,默认是7位数 -d # (min # of dig ...
- textarea 超过字数
<textarea name="></textarea> <div id="statementRowChk"></div> ...