题目链接:UVA - 437 The Tower of Babylon
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int x, y, z;
}a[10005];
int dp[10005];
bool cmp(struct node a, struct node b)
{
if(a.x > b.x)return true;
else if(a.x == b.x && a.y > b.y)return true;
else return false;
}
int main()
{
std::ios::sync_with_stdio(0);
int n, m, i, j, k, t = 1, x, y, z, p = 0;
while(scanf("%d", &n) != EOF)
{
if(n == 0)break;
p = 0;
memset(dp, 0, sizeof(dp));
for(i = 1; i <= n; i++)
{
scanf("%d %d %d", &x, &y, &z);
struct node w;
w.x = x; w.y = y; w.z = z; a[++p] = w;
w.x = x; w.y = z; w.z = y; a[++p] = w;
w.x = y; w.y = z; w.z = x; a[++p] = w;
w.x = y; w.y = x; w.z = z; a[++p] = w;
w.x = z; w.y = x; w.z = y; a[++p] = w;
w.x = z; w.y = y; w.z = x; a[++p] = w;
}
sort(a + 1, a + 1 + p, cmp);
int ans = 0;
for(i = 1; i <= p; i++)
{
dp[i] = a[i].z;
for(j = 1; j < i; j++)
{
if(a[j].x > a[i].x && a[j].y > a[i].y)dp[i] = max(dp[i], dp[j] + a[i].z);
}
ans = max(dp[i], ans);
}
printf("Case %d: maximum height = %d\n", t++, ans);
}
return 0;
}