How to eat more Banana(nyoj 232)

题目:点击打开链接

题目大意:提供给你长宽高不同的矩形块,每一种矩形块个数无限,现要求叠加矩形块使得能够到达的高度最高。注意,叠加矩形块要求下面的矩形块的长和宽分别严格大于上面的矩形的长和宽。

这题类似矩形嵌套题,我用了比较笨的方法……每输入一种矩形的尺寸,都将其长宽高的不同组合情况当作新的矩形加入数组。


#include 
#include 
#include 

struct node
{
	int x, y, z;
};

node block[500];
int dp[500];
int a, b, c, k;

void add()
{
	block[k].x = a;
	block[k].y = b;
	block[k++].z = c;
	
	block[k].x = a;
	block[k].y = c;
	block[k++].z = b;
	
	block[k].x = b;
	block[k].y = a;
	block[k++].z = c;
	
	block[k].x = b;
	block[k].y = c;
	block[k++].z = a;
	
	block[k].x = c;
	block[k].y = b;
	block[k++].z = a;
	
	block[k].x = c;
	block[k].y = a;
	block[k++].z = b;
}

int cmp(const void *p, const void *q)
{
	node *c = (node *)p;
	node *d = (node *)q;
	if(c->x != d->x)
		return c->x - d->x;
	else
		return c->y - d->y;
}


int main (void)
{
	int n, count = 0;
	while(scanf("%d", &n) != EOF)
	{
		if(n == 0)
			break;
		count ++;
		int i, j;
		k = 0;
		for(i = 0; i < n; i++)
		{
			scanf("%d %d %d", &a, &b, &c);
 			add();
		}	
		
		qsort(block, n * 6, sizeof(block[0]), cmp);
			
		int max = 1;
		dp[0] = block[0].z;
		for(i = 1; i < 6 * n; i++)
		{ 
			dp[i] = block[i].z;//初始值是这块矩形块的高 
			for(j = i - 1; j >= 0; j --)
			{
				if(block[i].x > block[j].x && block[i].y > block[j].y)
				{
					if(dp[i] < dp[j] + block[i].z)
						dp[i] = dp[j] + block[i].z;
				} 
			}

		}
		for(i = 0; i < 6 * n; i++)
		{
			if(max < dp[i])
				max = dp[i];
		}
	
		printf("Case %d: maximum height = %d\n", count, max);
	}
	return 0;
}

你可能感兴趣的:(NYOJ,动态规划)