一道基础DP。。但是我DP太菜了。。想了半天没思路,网上搜了搜大牛们的解法才顿悟,DP要加强啊,实在太弱了。。
#include<stdio.h>
#include<stdlib.h>
struct Block
{
int a, b, h;
};
static int max(int a, int b)
{
return a > b ? a : b;
}
static int min(int a, int b)
{
return a < b ? a : b;
}
static int cmp(const void *p1, const void *p2)
{
struct Block *b1 = (struct Block*) p1;
struct Block *b2 = (struct Block*) p2;
if (b1->a != b2->a)
return b1->a - b2->a;
else
return b1->b - b2->b;
}
int main()
{
int n, count = 0, dp[100];
struct Block *array = malloc(100 * sizeof(struct Block));
while (scanf("%d", &n), n)
{
int i, j, a, b, c;
for (i = 0; i < n; i++)
{
scanf("%d %d %d", &a, &b, &c);
array[i * 3].a = min(a, b);
array[i * 3].b = max(a, b);
array[i * 3].h = c;
array[i * 3 + 1].a = min(a, c);
array[i * 3 + 1].b = max(a, c);
array[i * 3 + 1].h = b;
array[i * 3 + 2].a = min(c, b);
array[i * 3 + 2].b = max(c, b);
array[i * 3 + 2].h = a;
}
int total = 3 * n;
qsort(array, total, sizeof(struct Block), cmp);
for (i = 0; i < total; i++)
dp[i] = array[i].h;
for (i = 1; i < total; i++)
for (j = 0; j < i; j++)
if (array[j].a < array[i].a && array[j].b < array[i].b)
dp[i] = max(dp[i], dp[j] + array[i].h);
int maxx = 0;
for (i = 0; i < total; i++)
if (dp[i] > maxx)
maxx = dp[i];
printf("Case %d: maximum height = %d\n", ++count, maxx);
}
free(array);
return 0;
}