Monkey and Banana

来源:互联网 发布:owncloud php网盘源码 编辑:程序博客网 时间:2024/06/06 01:57


一组研究人员正在设计一项实验,以测试猴子的智商。他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子。如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉。
 
研究人员有n种类型的砖块,每种类型的砖块都有无限个。第i块砖块的长宽高分别用xi,yi,zi来表示。 同时,由于砖块是可以旋转的,每个砖块的3条边可以组成6种不同的长宽高。
 
在构建塔时,当且仅当A砖块的长和宽都分别小于B砖块的长和宽时,A砖块才能放到B砖块的上面,因为必须留有一些空间让猴子来踩。
 
你的任务是编写一个程序,计算猴子们最高可以堆出的砖块们的高度。
Input
输入文件包含多组测试数据。
每个测试用例的第一行包含一个整数n,代表不同种类的砖块数目。n<=30.
接下来n行,每行3个数,分别表示砖块的长宽高。
当n= 0的时候,无需输出任何答案,测试结束。
Output
对于每组测试数据,输出最大高度。格式:Case 第几组数据: maximum height = 最大高度
Sample Input
1
10 20 30 

6 8 10 
5 5 5 

1 1 1 
2 2 2 
3 3 3 
4 4 4 
5 5 5 
6 6 6 
7 7 7 

31 41 59 
26 53 58 
97 93 23 
84 62 64 
33 83 27 
Sample Output
Case 1: maximum height = 40
Case 2: maximum height = 21 
Case 3: maximum height = 28 
Case 4: maximum height = 342 
#include <stdio.h>  #include <cstring>  #include <algorithm>  using namespace std;  #define CLR(a,b) memset(a,b,sizeof(a))  #define INF 0x3f3f3f3f  #define LL long long  struct node  {      int x,y,h;      //让x >= y   }a[200];  int ant;  bool cmp(node a,node b)  {      if (a.x == b.x)          return a.y > b.y;      return a.x > b.x;  }  void PushUp(int t1,int t2,int t3)  {      a[ant].h = t3;      a[ant].x = t1;      a[ant].y = t2;      ant++;      a[ant].h = t3;      a[ant].x = t2;      a[ant].y = t1;      ant++;      a[ant].h = t2;      a[ant].x = t1;      a[ant].y = t3;      ant++;      a[ant].h = t2;      a[ant].x = t3;      a[ant].y = t1;      ant++;      a[ant].h = t1;      a[ant].x = t2;      a[ant].y = t3;      ant++;      a[ant].h = t1;      a[ant].x = t3;      a[ant].y = t2;      ant++;  }  int main()  {      int n;      int ans;      int dp[200];      int Case = 1;      while (~scanf ("%d",&n) && n)      {          ant = 1;          for (int i = 1 ; i <= n ; i++)          {              int t1,t2,t3;              scanf ("%d %d %d",&t1,&t2,&t3);              PushUp(t1,t2,t3);          }          ant--;          printf ("Case %d: maximum height = ",Case++);          sort (a+1 , a+1+ant , cmp);          ans = 0;          for (int i = 1 ; i <= ant ; i++)          {              dp[i] = a[i].h;              for (int j = i - 1 ; j >= 1 ; j--)              {                  if (a[i].x < a[j].x && a[i].y < a[j].y)                      dp[i] = max (dp[i] , dp[j] + a[i].h);              }              ans = max (ans , dp[i]);          }          printf ("%d\n",ans);      }      return 0;  }  


原创粉丝点击