UVA 437(p269)----The Tower of Babylon

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct point
{
    int x,y,z;
    point(int a=0,int b=0,int c=0):x(a),y(b),z(c) {}
};
vector<point> g;
int n,f[1000];
int cmp(point a,point b)
{
    if(a.x==b.x) return a.y<b.y;
    else return a.x<b.x;
}
int main()
{
    int cas=0;
    while(scanf("%d",&n)==1&&n)
    {
        g.clear();
        int ans=0;
        for(int i=0; i<n; i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            g.push_back(point(x,y,z));
            g.push_back(point(y,z,x));
            g.push_back(point(x,z,y));
            g.push_back(point(y,x,z));
            g.push_back(point(z,y,x));
            g.push_back(point(z,x,y));
        }
        sort(g.begin(),g.end(),cmp);
        for(int i=0; i<g.size(); i++)
        {
            f[i]=g[i].z;
            for(int j=0; j<i; j++)
                if((g[j].x<g[i].x)&&(g[j].y<g[i].y))
                    f[i]=max(f[i],f[j]+g[i].z);
            if(f[i]>ans) ans=f[i];
        }
        printf("Case %d: maximum height = %d\n",++cas,ans);
    }
    return 0;
}
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=378

你可能感兴趣的:(UVA 437(p269)----The Tower of Babylon)