uva 437 - The Tower of Babylon(DP)
来源:互联网 发布:阿里云 马云 王坚 编辑:程序博客网 时间:2024/05/16 15:51
1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=378
2、题目:
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 . 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 and Output
The input file will contain one or more test cases. The first line of each test case contains an integer n, 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 , and .
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"
Sample Input
110 20 3026 8 105 5 571 1 12 2 23 3 34 4 45 5 56 6 67 7 7531 41 5926 53 5897 93 2384 62 6433 83 270
Sample Output
Case 1: maximum height = 40Case 2: maximum height = 21Case 3: maximum height = 28Case 4: maximum height = 3423、代码:
#include<stdio.h>#define N 200#include<algorithm>using namespace std;#include<string.h>#define INF 0x7fffffffstruct node{ int x; int y; int z; int area;;} a[N];int dp[N];int cmp(node a,node b){ return a.area>b.area;}int main(){ int n,cas=0,l,xx,yy,zz; while(scanf("%d",&n)!=EOF) { cas++; if(n==0) break; a[0].x=INF,a[0].y=INF,a[0].z=0,a[0].area=INF; int k=1; for(l=1; l<=n; l++) { scanf("%d%d%d",&xx,&yy,&zz); if(xx==yy && yy==zz) { a[k].x=xx,a[k].y=yy,a[k].z=zz,a[k].area=a[k].x*a[k].y,k++; } else if(xx==yy) { a[k].x=xx,a[k].y=yy,a[k].z=zz,a[k].area=a[k].x*a[k].y,k++; a[k].x=xx,a[k].y=zz,a[k].z=yy,a[k].area=a[k].x*a[k].y,k++; a[k].x=zz,a[k].y=xx,a[k].z=yy,a[k].area=a[k].x*a[k].y,k++; } else if(yy==zz) { a[k].x=xx,a[k].y=yy,a[k].z=zz,a[k].area=a[k].x*a[k].y,k++; a[k].x=yy,a[k].y=xx,a[k].z=zz,a[k].area=a[k].x*a[k].y,k++; a[k].x=yy,a[k].y=zz,a[k].z=xx,a[k].area=a[k].x*a[k].y,k++; } else if(xx==zz) { a[k].x=xx,a[k].y=yy,a[k].z=zz,a[k].area=a[k].x*a[k].y,k++; a[k].x=xx,a[k].y=zz,a[k].z=yy,a[k].area=a[k].x*a[k].y,k++; a[k].x=yy,a[k].y=xx,a[k].z=zz,a[k].area=a[k].x*a[k].y,k++; } else { a[k].x=xx,a[k].y=yy,a[k].z=zz,a[k].area=a[k].x*a[k].y,k++; a[k].x=xx,a[k].y=zz,a[k].z=yy,a[k].area=a[k].x*a[k].y,k++; a[k].x=yy,a[k].y=xx,a[k].z=zz,a[k].area=a[k].x*a[k].y,k++; a[k].x=yy,a[k].y=zz,a[k].z=xx,a[k].area=a[k].x*a[k].y,k++; a[k].x=zz,a[k].y=xx,a[k].z=yy,a[k].area=a[k].x*a[k].y,k++; a[k].x=zz,a[k].y=yy,a[k].z=xx,a[k].area=a[k].x*a[k].y,k++; } } n=k; //printf("*%d\n",n); sort(a+1,a+n,cmp);// for(int i=1; i<n; i++)// printf("*%d %d %d %d\n",a[i].area,a[i].x,a[i].y,a[i].z); memset(dp,0,sizeof(dp)); int tmp=-1; for(int i=0; i<n; i++) { for(int j=0; j<i; j++) { if((a[i].x<a[j].x) && (a[i].y<a[j].y)) { if(dp[j]+a[i].z>dp[i]) dp[i]=dp[j]+a[i].z; } } //dp[i]=max(dp[i-1],tmp); } int maxx=-1; for(int i=1; i<n; i++) { if(dp[i]>maxx) maxx=dp[i]; } printf("Case %d: maximum height = %d\n",cas,maxx); } return 0;}
- Uva-437 The Tower of Babylon DP
- UVA 437 The Tower of Babylon | dp
- uva--437The Tower of Babylon+dp
- UVA 437 The Tower of Babylon DP
- DP--UVA - 437 The Tower of Babylon
- UVa--437 The Tower of Babylon(dp)
- UVA 437 The Tower of Babylon 【dp】
- uva 437 The Tower of Babylon(DAG的DP)
- uva 437 - The Tower of Babylon(DP)
- Uva 437-The Tower of Babylon(DP)
- UVA 437-The Tower of Babylon(DP)
- UVa 437 - The Tower of Babylon(DP)
- UVA 437The Tower of Babylon (dp + DAG最长序列)
- UVa 437 The Tower of Babylon (DP&DAG)
- uva 437 The Tower of Babylon(dp,记忆化搜索)
- UVA 437 The Tower of Babylon DP+记忆化
- UVA 437 (POJ 2241) The Tower of Babylon(DP)
- uva 437 The Tower of Babylon (DAG_变形 ,dp)
- 银行业务调度系统II
- 许留山导入到SQL “文本被截断,或者一个或多个字符在目标代码页中没有匹配项”错误的解决
- 总结六年的嵌入式软件研发
- 关于MySQL的多用户问题
- String相关的面试题
- uva 437 - The Tower of Babylon(DP)
- U-Boot移植(二)——U-Boot编译过程分析(3)
- DIV+CSS来布局网站
- C++引用与指针的关系
- samba 4.0.7 devel 编程之用户管理篇
- ubuntu快速配置网卡bonding
- ASP.NET MVC
- 定位 与浮动
- 白话经典算法系列之七 堆与堆排序