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.对于一个点,搜索完了之后再加上他自己的高度.

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn=30+5;
 5 int n,kase;
 6 int a[maxn][3],dp[maxn][3];
 7
 8 int dfs(int idx,int k){
 9     int &ans=dp[idx][k];
10     if(ans) return ans;
11     int x1=a[idx][(k+1)%3],y1=a[idx][(k+2)%3];
12     for(int i=1;i<=n;i++)
13         for(int j=0;j<3;j++){
14             int x2=a[i][(j+1)%3],y2=a[i][(j+2)%3];
15             if(x2<x1&&y2<y1||x2<y1&&y2<x1) ans=max(ans,dfs(i,j));
16         }
17     ans+=a[idx][k];
18     return ans;
19 }
20 void init(){
21     memset(dp,0,sizeof dp);
22     for(int i=1;i<=n;i++)
23         for(int j=0;j<3;j++)
24             scanf("%d",&a[i][j]);
25 }
26 int main(){
27     while(scanf("%d",&n)&&n){
28         init();
29         int ans=0;
30         for(int i=1;i<=n;i++)
31             for(int j=0;j<3;j++)
32                 ans=max(ans,dfs(i,j));
33         printf("Case %d: maximum height = %d\n",++kase,ans);
34     }
35     return 0;
36 }

时间: 2024-10-07 22:32:15

UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)的相关文章

sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)

DescriptionIn the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. T

POJ3249 Test for Job 【DAG】+【记忆化搜索】

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9201   Accepted: 2080 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job

Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)

描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友

动态规划①——记忆化搜索

首先的首先,必须明白动态规划(DP)以后很有用很有用很有用很有用……首先的其次,必须明白:动规≍搜索=枚举 一.最简单的记忆化搜索(应该可以算DP) 题目(来自洛谷OJ)http://www.luogu.org/problem/show?pid=1434# [不麻烦大家自己找了]题目描述 DescriptionMichael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最

由DAG到背包问题——记忆化搜索和递推两种解法

一.问题描述 物品无限的背包问题:有n种物品,每种均有无穷多个.第 i 种物品的体积为Vi,重量为Wi.选一些物品装到一个容量为 C 的背包中,求使得背包内物品总体积不超过C的前提下重量的最大值.1≤n≤100, 1≤Vi≤C≤10000, 1≤Wi≤1000000. 二.解题思路 我们可以先求体积恰好为 i 时的最大重量(设为d[i]),然后取d[i]中的最大值(i ≤ C).与之前硬币问题,"面值恰好为S"就类似了.只不过加了新属性--重量,相当于把原来的无权图改成带权图,即把&q

着色方案(动态规划+记忆化搜索)

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1079 AC代码: 1 /* 2 直接状态压缩是显然是不可行的,我们考虑如果没有相邻颜色不相同的限制的话, 3 如果两种油漆能染的木块数目相同,我们就可以认为两种油漆无差别. 4 设dp[a1][a2][a3][a4][a5]为能染1个木块的油漆有a1种……的方案数. 5 但是有相邻颜色的限制,如果上一次用了颜色数为k的油漆, 6 那么这一次有一种颜色数为k-1的油漆就不能用了,转移的时

动态规划-记忆化搜索

1.数字三角形 学习链接:http://blog.csdn.net/zwhlxl/article/details/46225947 输入样例: 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 输出样例: 30 递归代码: #include <stdio.h> #include <memory.h> #include <math.h> #include <string> #include <vector> #include <

LightOJ1417 Forwarding Emails(强连通分量+缩点+记忆化搜索)

题目大概是,每个人收到信息后会把信息发给他认识的一个人如此下去,问一开始要把信息发送给谁这样看到信息的人数最多. 首先找出图中的SCC并记录每个SCC里面的点数,如果传到一个SCC,那么里面的人都可以看到信息. 然后SCC缩点后就形成DAG,直接记忆化搜索,d(u)搜索从u点出发开始传最多能传多少人. 最后就是找答案了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namesp

poj1351Number of Locks(记忆化搜索)

题目链接: 传送门 思路: 这道题是维基百科上面的记忆化搜索的例题... 四维状态dp[maxn][5][2][5]分别表示第几根棒子,这根棒子的高度,是否达到题目的要求和使用不同棒子数,那么接下来就是状态转移了...要用到位运算判断以前是否这种高度的棒子用到没...那么这个问题就解决了... 题目: Number of Locks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1126   Accepted: 551