题目链接:UVa437巴比伦塔
题解:
这个就是矩形嵌套的改变,把2D变为3D了,然后加1变为加上他们的高度
注意点是一个矩形可以有三种不同的形态
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n;
int p; //保存各个矩形的数量,一个矩形有三种
struct jux{
int x,y;
int h;
}ju[910];
int gra[910][910];
int dp[910];
bool isqian(jux a, jux b){
if((a.x>b.x && a.y>b.y) || (a.y>b.x&&a.x>b.y)) return true;
return false;
}
void build(int x,int y, int z){
ju[p].x = x;
ju[p].y = y;
ju[p].h = z;
for(int i = 0; i < p; i++){
if(isqian(ju[i],ju[p])) gra[i][p] = 1;
if(isqian(ju[p],ju[i])) gra[p][i] = 1;
}
}
int d(int pos){
int &ans = dp[pos];
if(ans) return ans;
ans = ju[pos].h;
for(int i = 0; i < 905;i++){
if(gra[pos][i]) ans = max(ans,d(i)+ju[pos].h);
}
return ans;
}
int main(){
int rnd = 0;
while(scanf("%d",&n) == 1 && n){
memset(gra,0,sizeof(gra));
memset(dp,0,sizeof(dp));
p = 0;
int x,y,z;
for(int i = 0; i < n; i++){
scanf("%d%d%d",&x,&y,&z);
build(x,y,z);
p++;
build(x,z,y);
p++;
build(y,z,x);
p++;
}
int maxn = -1;
for(int i = 0; i < p; i++){
if(!dp[i]) d(i);
if(maxn < dp[i]) maxn = dp[i];
}
printf("Case %d: maximum height = %d\n",++rnd,maxn);
}
return 0;
}
本文提供UVa437巴比伦塔的解题思路及代码实现,采用三维矩形嵌套算法,通过动态规划求解最大高度。包括矩形结构定义、嵌套判断、构建图形关系矩阵及动态规划求解过程。
405

被折叠的 条评论
为什么被折叠?



