UVA 437 the Tower of Babylon

Source: Internet
Author: User

Test instructions: There are n cubes, to be piled into a master as high as possible, each cube is placed above the length of the cube is strictly less than below, each cube has an unlimited

Analysis: My approach is that each cube has a maximum of 6 states, then also have a 6n fixed length and width of the cube, then this is a DAG on the longest road, 6n Cube, O (n^2) algorithm,

Very simple, see the code for details

#include <bits/stdc++.h>using namespacestd;Const intmaxn=205;intM,N,DP[MAXN];structnode{intx, Y, Z voidInitintAintBintc) {x=a;y=b;z=C; }}P[MAXN];BOOLCMP (node A,node b) {returna.x*a.y<b.x*b.y;}BOOLRead () {scanf ("%d",&N); if(!n)return false; M=0;  while(n--){        intA,b,c; scanf ("%d%d%d",&a,&b,&c); P[m++].init (A,B,C); P[m++].init (B,A,C); P[m++].init (A,C,B); P[m++].init (b,c,a); P[m++].init (C,A,B); P[m++].init (c,b,a); } sort (P,p+m,cmp); return true;}voidsolve () {intans=0;  for(intI=0; i<m;i++) {Dp[i]=p[i].z;  for(intj=0; j<i;j++)          if(p[i].x>p[j].x&&p[i].y>p[j].y) Dp[i]=max (dp[i],dp[j]+p[i].z); Ans=Max (ans,dp[i]); } printf ("%d\n", ans);}intMain () {intcas=1;  while(Read ()) {printf ("Case %d:maximum height =", cas++);    Solve (); }    return 0;}
View Code


Here is the code for Rujia

//UVa437 the Tower of Babylon//Rujia Liu//algorithm: The longest path on a dag with a status of (IDX, k), that is, the current top is a cube idx, where the K bar (after sorting) is high#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineREP (i,n) for (int i = 0; I < (n); i++)Const intMAXN = -+5;intN, blocks[maxn][3], d[maxn][3];voidGet_dimensions (int* V,intBintDim) {intIDX =0; REP (i,3)if(I! = Dim) v[idx++] =blocks[b][i];}intdpintIintj) {int& ans =D[i][j]; if(Ans >0)returnans; Ans=0; intv[2], v2[2];  Get_dimensions (V, I, j); Rep (A,n) rep (b,3) {get_dimensions (v2, A, b); if(v2[0] < v[0] && v2[1] < v[1]) ans =Max (ans, DP (A, b)); } ans+=Blocks[i][j]; returnans;}intMain () {intKase =0;  while(SCANF ("%d", &n) = =1&&N) {rep (i,n) {Rep (J,3) scanf ("%d", &Blocks[i][j]); Sort (blocks[i], blocks[i]+3); } memset (d,0,sizeof(d)); intAns =0; Rep (I,n) Rep (J,3) ans =Max (ans, DP (I,J)); printf ("Case %d:maximum height =%d\n", ++Kase, ans); }  return 0;}
View Code

UVA 437 the Tower of Babylon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.