UVA - 437 The Tower of Babylon(SPFA解拓扑)

本文介绍了一种使用C++编程实现的算法,解决三维盒子堆叠问题,通过动态规划找到所有盒子组合中最大高度。通过输入盒子尺寸,计算并输出每个案例的最大堆叠高度,适合于计算机图形学和优化算法应用。

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

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

#define INF 0x3f3f3f3f
#define MAXN 30*3+5

using namespace std;

struct Box {
	int x,y,z;
	Box() = default;
	Box(int x, int y, int z):x(x),y(y),z(z){}
	bool operator < (const Box &b) const {
		return this->x<b.x && this->y<b.y;
	}
};

int n;
int dp[MAXN];
vector<int> E[MAXN];
queue<int> q;
int cnt = 0;

inline void init()
{
	for (auto &v : E) v.clear();
	while(!q.empty()) q.pop();
	++cnt;
}

int main()
{
	int a[3];
	while(~scanf("%d",&n) && n)
	{
		init();		
		Box box[MAXN];
		for(int i=0; i<n; ++i)
		{
			scanf("%d%d%d",&a[0],&a[1],&a[2]);
			sort(a,a+3);
			box[3*i] = Box(a[0],a[1],a[2]);
			box[3*i+1] = Box(a[0],a[2],a[1]);
			box[3*i+2] = Box(a[1],a[2],a[0]);
		}
		for(int i=0; i<3*n; ++i)
			for(int j=0; j<3*n; ++j)
				if (box[i]<box[j])
					E[i].push_back(j);
		
		int maxx = -INF;
		for (int i=0; i<3*n; ++i)
		{
			memset(dp, 0, sizeof(dp));
			dp[i] = box[i].z;
			q.push(i);
			while(!q.empty())
			{
				int cur = q.front();
				q.pop();
				for (auto &e : E[cur])
				{
					if (dp[e] < dp[cur]+box[e].z)
					{
						dp[e] = dp[cur]+box[e].z;
						q.push(e);
					}
				}
			}			
			for (int i=0; i<3*n; ++i)
				maxx = max(maxx, dp[i]);			
		}
		printf("Case %d: maximum height = %d\n",cnt ,maxx);	
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值