UVa 437 巴比伦塔(The Tower of Babylon)详细题解

来源:互联网 发布:淘宝网冬之恋羊绒线 编辑:程序博客网 时间:2024/05/24 03:24

累加器传送门:

http://blog.csdn.net/NOIAu/article/details/71775000

题目传送门:https://vjudge.net/problem/UVA-437

题目:

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 (xi; yi; zi). 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.

输入:

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 xi, yi and zi. 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’

样例输入:

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

样例输出:

Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342

这道题我们一开始应该就能想到要用最上层是哪个面来设计dp转移,比如dp[i][j]表示最上面的面的尺寸是i* j的,这样或许是可以的,但是有一个问题,长,宽,高可能会很大,这样一来很可能会MLE,那怎么办呢,我们可以转化一下思想,用dp[i][j]来表示第i个长方体的第j号边作为最上面位置的高,什么叫第j号边呢?比如一个长方体的长宽高,我们一次编上号,1,2,3号,这样长就是1号,宽就是2号,高就是3号,编好号之后,就无所谓长宽高了,因为这三条边每条边都可以作为高,也都可以作为宽或者长,这里我们只用讨论高就行了,记得在编好之前进行一次排序,让单组的数据是有序的,这样便于我们之后的比较
现在我们先看一份代码,这份代码是递推的,并且是错误的代码,请问有人知道这份代码错误在哪里吗?(如果不想看,也可以直接跳到后面的记忆化搜索,但这对理解递推和记忆化搜索求DAG最长路上的不同有一定的帮助)

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#define MAXN 35using namespace std;int tail;int dp[MAXN][5],a[MAXN][5];int n;bool init(){    scanf("%d",&n);    if(!n) return false;    for(int i=1;i<=n;i++){        for(int j=1;j<=3;j++){            scanf("%d",&a[i][j]);        }        sort(a[i],a[i]+4);        dp[i][1]=a[i][1];        dp[i][2]=a[i][2];        dp[i][3]=a[i][3];    }    return true;}void dpp(){    for(int i=1;i<=n;i++){        for(int x=1;x<=3;x++){            for(int j=1;j<=n;j++){                for(int y=1;y<=3;y++){                    int tail_one=0;                    int tail_two=0;                    int te[3];int mp[3];                    for(int temp=1;temp<=3;temp++){                        if(temp!=x) te[++tail_one]=a[i][temp];                        if(temp!=y) mp[++tail_two]=a[j][temp];                    }                    if(te[1]<mp[1]&&te[2]<mp[2]){                        dp[i][x]=max(dp[j][y]+a[i][x],dp[i][x]);                    }                }            }           }    }}void print(){    int maxn=-1;    for(int i=1;i<=n;i++){        for(int j=1;j<=3;j++){            maxn=max(dp[i][j],maxn);        }    }    printf("Case %d: maximum height = %d\n",++tail,maxn);}int main(){    freopen("in.in","r",stdin);    while(init()){        dpp();        print();    }    return 0;}

这个问题可能不是很容易看出来,因为每次好像都枚举了所有的以i为顶面长方体,i的第x条边为高,j为下面的长方体,j的第y条边为高的情况,而且dp转移好像也没有问题?
那么问题到底在哪里呢?
大家可以模拟一下这组数据

输入:
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
输出:
Case 3: maximum height = 28

真是辛苦了看了上面那么丑的代码的人了…
应该可以看出错在哪里了吧,举个例子,对于i=1的情况,下地面在切换的时候,会一直进行更新,最后会变成上面是1 *1 *1的方块,下面就直接是7 *7 *7的方块,而不是1过了2过了3过了4…

所以我们在进行递推的时候,一定要注意检查方程,检查递推顺寻,以及检查这道题是否可以用递推来处理

“记忆化搜索”

直接上代码
很明显,这里用一个G数组模拟邻接矩阵来判断可不可以走,把每个立方体变成三个立方体(三种让长小于宽的方式)

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#define MAXN 35using namespace std;int tail,t;int dp[MAXN*3+10][MAXN*3+10],a[MAXN][5];bool G[MAXN*3][MAXN*3]; struct Node{    int x,y,z;}node[MAXN*3+10];int n;void judge(){    memset(G,false,sizeof(G));    for(int i=1;i<=3*n;i++){        for(int j=1;j<=3*n;j++){            if(node[i].x<node[j].x&&node[i].y<node[j].y)            G[i][j]=true;        }    }}bool init(){    t=0;    scanf("%d",&n);    if(!n) return false;    for(int i=1;i<=n;i++){        for(int j=1;j<=3;j++){            scanf("%d",&a[i][j]);        }        sort(a[i],a[i]+4);        node[++t].x=a[i][1];        node[t].y=a[i][2];        node[t].z=a[i][3];        node[++t].x=a[i][2];        node[t].y=a[i][3];        node[t].z=a[i][1];        node[++t].x=a[i][1];        node[t].y=a[i][3];        node[t].z=a[i][2];     }    memset(dp,-1,sizeof(dp));    return true;}int dpp(int top,int down){    int& ans =dp[top][down];    if(ans!=-1) return ans;    for(int i=1;i<=3*n;i++){        if(down==i) continue;        if(G[down][i])        ans=max(ans,dpp(down,i)+node[top].z);    }    if(ans==-1) ans=node[top].z+node[down].z;     return ans;}int main(){    while(init()){        judge();        int maxn=-1;        for(int i=1;i<=3*n;i++){            for(int j=1;j<=3*n;j++){                if(G[i][j]){                    maxn=max(maxn,dpp(i,j));                }            }        }        printf("Case %d: maximum height = %d\n",++tail,maxn);    }    return 0;}

这里写图片描述

0 0
原创粉丝点击