POJ-2241(简单动态规划)(The Tower of Babylon )

/*==========================================================*\
| [题目描述]有N种石头(每种数量无限)题目给出每种的长宽高
| 先要求将其按底面积递减的顺序从下往上堆(注意是严格递减,对应边也不能相等) 
| 问最多可以堆多高?
| [解题思路]分析:将每块石头变成3块,block代表所有石头,有3个成员x,y,z. 然后将其按照底面积大小从大到小排序。
| 建立一个数组h[],h[i]记录的是当前石头作为顶上石头时候的总高度。于是状态转移方程为 h[i] = max {h[j]+block[i].z)。
| 输出最大的height[i]就可以了
\*==========================================================*/
struct my
{
	int x, y, z;
} wo[1000];
bool cmp(my a, my b)
{
	return a.x * a.y > b.x * b.y;
}
int main()
{
	int n;
	int cases = 1;
	while (cin>>n, n) {
		int i, j, k, index = 0;
		for (i = 0; i < n; ++i) {
			cin>>wo[index].x>>wo[index].y>>wo[index].z;
			++index;
			wo[index].x = wo[index - 1].y, wo[index].y = wo[index - 1].z, wo[index].z = wo[index - 1].x;
			++index;
			wo[index].x = wo[index - 2].x, wo[index].y = wo[index - 2].z, wo[index].z = wo[index - 2].y;
			++index;
		}
		sort(wo, wo + index, cmp);
		int h[1000];
		h[0] = wo[0].z;
		int ans = h[0];
		for (i = 1; i < index; ++i) {
			int max = 0;
			for (j = 0; j < i; ++j) {
				if (((wo[j].x > wo[i].x && wo[j].y > wo[i].y) || (wo[j].y > wo[i].x && wo[j].x > wo[i].y)) && max < h[j])
					max = h[j];
			}
			h[i] = max + wo[i].z;
			if (h[i] > ans) ans = h[i];
		}
		int max = 0;
		printf("Case %d: maximum height = %d\n", cases++, ans);
	}
    return 0;
}

你可能感兴趣的:(POJ-2241(简单动态规划)(The Tower of Babylon ))