讲道理 还是不是很会DP
主要是分解问题,逐个解决,寻找关联,得到结果 求最优解
这题的题目大意是 : 给你N种立方体 每种无限多个 让你把给出的立方体垒起来,要求高度最高 而且每两个立方体之间必须上上面的接触面积小于下面的接触面积
立方体的长宽高是给出的
我的做法: 按照立方体的长宽高先排序,规则是每个立方体那个最小面积从小到大排序
然后从最大的开始一个一个开始往上找
记录状态的过程和搜索下个目标的过程,找到一个之前的就不管了
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int m, n, x, y,z,dp[200];
struct node{
int x, y, z;
void f(int a, int b, int c){
x = a; y = b; z = c;
}
}st[200];
bool cmp(node a, node b){
return a.x*a.y < b.x*b.y;
}
int main(){
int flag = 1;
while (scanf("%d", &n) && n){
m = 0;
int i, j;
for (i = 0; i < n; i++){
scanf("%d%d%d", &x, &y, &z);
st[m++].f(x, y, z);
st[m++].f(x, z, y);
st[m++].f(y, x, z);
st[m++].f(y, z, x);
st[m++].f(z, y, x);
st[m++].f(z, x, y);
}
sort(st, st + m, cmp);
int t = 0;
for (i = 0; i < m; i++){
dp[i] = st[i].z;
for (j = 0; j < i;j++)
if (st[i].x>st[j].x&&st[i].y>st[j].y)
dp[i] = max(dp[i], dp[j] + st[i].z);
if (dp[i] > t)
t = dp[i];
}
printf("Case %d: maximum height = %d\n", flag++, t);
}
return 0;
}