对于输入的每一个砖头,将其按照不同的情况放置,并且将所有可以放置的情况放在vector中进行排序,排序之后每次取出一块,看是否能够放在另外几块之上,如果能够放置,那么就更新相应的高度,最终记录最大高度并且进行输出就行了,具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;
int n;
struct node{
int x, y;
int z;
node(int a, int b, int c){
x = a; y = b; z = c;
}
bool operator< (const node &temp) const{
if (x != temp.x) return x>temp.x;
return y > temp.y;
}
};
int main(){
int Case = 1;
while (cin >> n&&n){
vector<node> v;
for (int i = 0; i < n; i++){
int a, b, c;
cin >> a >> b >> c;
v.push_back(node(a,b,c));
v.push_back(node(a,c,b));
v.push_back(node(b,a,c));
v.push_back(node(b,c,a));
v.push_back(node(c,a,b));
v.push_back(node(c,b,a));
}
int ans = -1;
sort(v.begin(),v.end());
int*height = new int[v.size()];
memset(height,0,sizeof(height));
for (int i = 0; i < v.size(); i++){
height[i] = v[i].z;
for (int j = 0; j < i; j++){
if (v[j].x > v[i].x&&v[j].y > v[i].y){
height[i] = max(height[i],v[i].z+height[j]);
}
}
ans = max(ans,height[i]);
}
cout << "Case " << Case++ << ": maximum height = " << ans << endl;
}
return 0;
}