首页 > 代码库 > ZOJ1093 动态规划
ZOJ1093 动态规划
给你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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。