The Tower of Babylon UVA - 437

本文介绍了一种通过排序和动态规划解决砖块堆叠问题的方法,旨在寻找砖块堆叠的最大高度。通过对每种可能的砖块放置方式进行预处理并排序,使用动态规划更新堆叠高度,从而得出最大堆叠高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于输入的每一个砖头,将其按照不同的情况放置,并且将所有可以放置的情况放在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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值