The Tower of Babylon(UVa 437)

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

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

DAG最长路算法:

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

本题代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=30+5;
int blocks[maxn][3],dist[maxn][3],n;

void get_dimensions(int* v, int b, int dim) {
  int idx = 0;
  for(int i=0;i<3;i++)
   if(i != dim) v[idx++] = blocks[b][i];///找出每个方块对应的长,宽,高
}

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

int main()
{
    int kase=0;
    while(~scanf("%d",&n)&&n){
        for(int i=0;i<n;i++){
            for(int j=0;j<3;j++) scanf("%d",&blocks[i][j]);
        sort(blocks[i],blocks[i]+3);
        }
        memset(dist,0,sizeof(dist));
        int ans=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<3;j++){///枚举每个方块的每种摆放位置
                ans=max(ans,dp(i,j));
            }
        printf("Case %d: maximum height = %d\n",++kase,ans);
    }
    return 0;
}
时间: 2024-10-18 06:07:51

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

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

题目:题目链接 思路:每个方块可以用任意多次,但因为底面限制,每个方块每个放置方式选一个就够了,以x y为底 z 为高,以x z为底 y 为高,以y z为底 x为高,因为数据量很小,完全可以把每一种当成DAG上的一个结点,然后建图找最长路径. AC代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <cs

UVA 437 十九 The Tower of Babylon

The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 437 Appoint description:  System Crawler  (2015-08-29) Description Perhaps you have heard of the legend of the Tower of Babylon. No

uva 437 The Tower of Babylon(动态规划 嵌套矩形问题最长路)

有思路就去做,要相信自己 多处理更复杂的情况,你就不觉得现在复杂了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; struct soli { ll a,b,c; }s[40]; int n; ll d[40][3]; int vis[40][3]; ll answer[40][3]; ll dp(int s1,int s2

UVa 437 The Tower of Babylon

Description 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 typ

UVA 437 The Tower of Babylon DP

有向图DAG The Tower of Babylon Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So n

UVa 437 The Tower of Babylon(动态规划)

传送门 Description 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

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

 题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于另一个时  这个长方体可以放在另一个上面  要求输出这样累积起来的最大高度 因为每个长方体都有3种放法  比较不好控制   可以把一个长宽高分成三个长方体  高度是固定的  这样就比较好控制了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define maxn 105 int x[max

uva 动态规划 437 The Tower of Babylon

The Tower of Babylon Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in

UVA The Tower of Babylon

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