poj 2241 疊方塊 基本動態規劃

題意:給定n種block,每種block有無限多個,每個block有x, y, z三個屬性

要求疊起來,使得在滿足下面的長和寬嚴格大於上面的,情況下,高度最高


解法:

d[i] 表示 以第i個物品爲能達到的最大高度

轉移方程 d[i] = max{ d[i] ,  d[x] + height(x) 其中x要滿足題目要求約束,遍取 0 - n - 1}

max(d[x])就是答案

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定義
const int  INF = 990000000;
const int maxn = 500 ;
const int MAXN = maxn;
///全局變量 和 函數
//int T;
int max(int a, int b)
{
	return a > b ? a : b;
}

int n;
struct BLOCK
{
	int x, y, z;
	bool operator < (const BLOCK& t) const
	{
		return (x * y) > (t.x * t.y);
	}
};
BLOCK blocks[maxn * 5];
int d[maxn * 5];
bool vis[maxn * 5];
int cnt;
int dp(int k)
{
	if (vis[k])
		return d[k];
	vis[k] = true;
	int i, j;
	int nowy, nowx, mh, maxheight;
	//以x, y爲底

	mh = blocks[k].z;
	nowy = blocks[k].y;
	nowx = blocks[k].x;
	maxheight = mh;
	for (i = cnt - 1; i >= 0; i--)
	{
		if ( (nowy < blocks[i].y && nowx < blocks[i].x) || (nowx < blocks[i].y && nowy < blocks[i].x) ) //寫錯了好幾次
		{
			maxheight = max(maxheight, dp(i) + mh);
		}
	}
	return d[k] = maxheight;
}
int main()
{
	///變量定義
	int i, j;	
	int cases = 1;
	while(1)
	{
		memset(vis, false, sizeof(vis));
		scanf("%d", &n);
		if (n == 0)
			break;
		cnt = 0;
		for (i = 0; i < n; i++)
		{
			int x, y, z;
			scanf("%d %d %d", &x, &y, &z);

			blocks[cnt].x = x;
			blocks[cnt].y = y;
			blocks[cnt].z = z;
			cnt++;

			blocks[cnt].x = x;
			blocks[cnt].y = z;
			blocks[cnt].z = y;
			cnt++;

			blocks[cnt].x = y;
			blocks[cnt].y = z;
			blocks[cnt].z = x;
			cnt++;
		}
//		sort(blocks, blocks + cnt);
//		vis[0] = true;
//		d[0] = blocks[0].z;
		int ans = -1;
		for (i = cnt - 1; i >= 0; i--)
		{
			ans = max(ans, dp(i));
		}
		printf("Case %d: maximum height = %d\n", cases++, ans);
	}
	
	///結束
	return 0;
}



在此基礎上上更改一下細節處理,對於block的面積從大到小進行排序

那麼d[i] = max{ d[i],  d[k]  + height[k]  k < i }

速度會更快

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定義
const int  INF = 990000000;
const int maxn = 500 ;
const int MAXN = maxn;
///全局變量 和 函數
//int T;
int max(int a, int b)
{
	return a > b ? a : b;
}

int n;
struct BLOCK
{
	int x, y, z;
	bool operator < (const BLOCK& t) const
	{
		return (x * y) > (t.x * t.y);
	}
};
BLOCK blocks[maxn * 5];
int d[maxn * 5];
bool vis[maxn * 5];
int cnt;
int dp(int k)
{
	if (vis[k])
		return d[k];
	vis[k] = true;
	int i, j;
	int nowy, nowx, mh, maxheight;
	//以x, y爲底

	mh = blocks[k].z;
	nowy = blocks[k].y;
	nowx = blocks[k].x;
	maxheight = mh;
	for (i = k - 1; i >= 0; i--)
	{
		if ( (nowy < blocks[i].y && nowx < blocks[i].x) || (nowx < blocks[i].y && nowy < blocks[i].x) ) //寫錯了好幾次
		{
			maxheight = max(maxheight, dp(i) + mh);
		}
	}
	return d[k] = maxheight;
}
int main()
{
	///變量定義
	int i, j;	
	int cases = 1;
	while(1)
	{
		memset(vis, false, sizeof(vis));
		scanf("%d", &n);
		if (n == 0)
			break;
		cnt = 0;
		for (i = 0; i < n; i++)
		{
			int x, y, z;
			scanf("%d %d %d", &x, &y, &z);

			blocks[cnt].x = x;
			blocks[cnt].y = y;
			blocks[cnt].z = z;
			cnt++;

			blocks[cnt].x = x;
			blocks[cnt].y = z;
			blocks[cnt].z = y;
			cnt++;

			blocks[cnt].x = y;
			blocks[cnt].y = z;
			blocks[cnt].z = x;
			cnt++;
		}
		sort(blocks, blocks + cnt); //先進行排序
		vis[0] = true;              //面積最大的作爲頂的最大高度,記憶化搜索
		d[0] = blocks[0].z;
		int ans = -1;
		for (i = cnt - 1; i >= 0; i--)
		{
			ans = max(ans, dp(i));
		}
		printf("Case %d: maximum height = %d\n", cases++, ans);
	}
	
	///結束
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章