码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ1093 动态规划

时间:2014-06-21 21:27:31      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   2014   string   os   

给你n中砖块,有三维长宽高,每种无限取用,叠加的条件是上一块的长宽必须严格大于下一块的长宽,求叠加最高高度,

思路:

把每种砖块最终按照放置方法可以转为六种,然后对于长和宽进行排序,这样就是LIS的变向问题了


#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#include<cctype>

#define ll long long

#define LL __int64

#define eps 1e-8

#define inf 0xfffffff

//const LL INF = 1LL<<61;

using namespace std;

//vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;

int box[500][3];
int dp[1000];

int cnt;

typedef struct Node {
	int x,y,z;
};

Node node[500];

void init() {
	memset(box,0,sizeof(box));
	memset(dp,0,sizeof(dp));
	cnt = 0;
}

/*void cal(int x,int y,int z) {
box[cnt][0] = x,box[cnt][1] = y,box[cnt++][2] = z;
box[cnt][0] = y,box[cnt][1] = x,box[cnt++][2] = z;
box[cnt][0] = y,box[cnt][1] = z,box[cnt++][2] = x;
box[cnt][0] = z,box[cnt][1] = y,box[cnt++][2] = x;
box[cnt][0] = x,box[cnt][1] = z,box[cnt++][2] = y;
box[cnt][0] = z,box[cnt][1] = x,box[cnt++][2] = y;
}*/

void cal(int x,int y,int z) {
	node[cnt].x = x,node[cnt].y = y,node[cnt++].z = z;
	node[cnt].x = y,node[cnt].y = x,node[cnt++].z = z;
	node[cnt].x = y,node[cnt].y = z,node[cnt++].z = x;
	node[cnt].x = z,node[cnt].y = y,node[cnt++].z = x;
	node[cnt].x = x,node[cnt].y = z,node[cnt++].z = y;
	node[cnt].x = z,node[cnt].y = x,node[cnt++].z = y;
}

bool cmp(Node x,Node y) {
	if(x.x == y.x) {
		if(x.y == y.y)return x.z < y.z;
		return x.y < y.y;
	} 
	return x.x < y.x;
}

int main() {
	int n;
	int	Case = 0;
	while(scanf("%d",&n),n) {
		init();
		for(int i=0;i<n;i++) {
			int x,y,z;
			scanf("%d %d %d",&x,&y,&z);
			cal(x,y,z);
		}
		int ans = 0;
		sort(node,node+cnt,cmp);
		/*for(int i=0;i<cnt;i++) {
			printf("%d %d %d**\n",node[i].x,node[i].y,node[i].z);
		}*/
		for(int i=0;i<cnt;i++)
			dp[i] = node[i].z;
		for(int i=0;i<cnt;i++) {
			for(int j=1;j<i;j++) {
				int a = node[j].x;
				int b= node[i].x;
				int c = node[j].y;
				int d= node[i].y;
				if(node[j].x < node[i].x && node[j].y < node[i].y) {
					dp[i] = max(dp[j] + node[i].z,dp[i]);
				}
			}
			if(ans < dp[i])
				ans = dp[i];
		}
		printf("Case %d: maximum height = %d\n",++Case,ans);
	}
	return 0;
}


ZOJ1093 动态规划,布布扣,bubuko.com

ZOJ1093 动态规划

标签:class   blog   code   2014   string   os   

原文地址:http://blog.csdn.net/yitiaodacaidog/article/details/32729307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!