题意:

一堆石头,给定长宽高,每种石头均可以使用无数次,问这堆石头可以叠放的最高高度,要求下面的石头的长和宽分别严格大于上面石头的长和宽。

分析:

采用DAG最长路算法,由于长宽较大,不能直接用于表示状态,因此采用d[i][x]表示以第i块石头为最高点,以其第x个边为高所能达到的最大高度,其中i为石头标号,x代表长/宽/高,然后根据长宽高要求构造DAG,最后记忆化搜索求出最长路。

代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int maxn = 0, ans = 0, n;
int y[35][4];
int G[35][4][35][4];
const int INF = 0x3fffffff;
int a[2],b[2], d[35][4];
int getDAG(int i, int k, int j, int m)
{
if(k==1) a[0] = y[i][2], a[1] = y[i][3];
else if(k==2) a[0] = y[i][3], a[1] = y[i][1];
else a[0] = y[i][2], a[1] = y[i][1]; if(m==1) b[0] = y[j][2], b[1] = y[j][3];
else if(m==2) b[0] = y[j][3], b[1] = y[j][1];
else b[0] = y[j][2], b[1] = y[j][1]; sort(a,a+2);
sort(b,b+2); return (a[0]>b[0]&&a[1]>b[1])?1:0; }
int dp(int i, int k)
{
int& ans = d[i][k];
if(ans > 0) return ans;
for(int j = 0; j < n; j++){
for(int m = 1; m <= 3; m++){
if(getDAG(j,m,i,k)) ans = max(ans, dp(j, m));
}
}
ans += y[i][k];
return ans;
}
int main (void)
{
int a, b, c;
cin>>n;
int cnt = 1;
while(n){
maxn = 0;
memset(d,0,sizeof(d));
memset(G,0,sizeof(G));
for(int i = 0; i < n; i++){
cin>>a>>b>>c;
y[i][1] = a; y[i][2] = b; y[i][3] = c;
}
for(int i = 0; i < n; i++)
for(int k = 1; k <= 3; k++)
for(int j = 0; j < n; j++)
for(int m = 1; m <= 3; m++)
getDAG(i,k,j,m); for(int i = 0; i < n; i++)
for(int k = 1; k <= 3; k++)
maxn = max(maxn,dp(i, k)); cout<<"Case "<<cnt<<": maximum height = "<<maxn<<endl;
cin>>n;
cnt++;
}
}

很白痴的细节问题纠结了一下午,还是要细心!细心!

UVA 437_The Tower of Babylon的更多相关文章

  1. uva The Tower of Babylon[LIS][dp]

    转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...

  2. UVA The Tower of Babylon

    The Tower of Babylon Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many det ...

  3. UVA 437 十九 The Tower of Babylon

    The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  4. UVa 437 The Tower of Babylon(经典动态规划)

    传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...

  5. UVa 437 The Tower of Babylon

    Description   Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...

  6. UVa 437 The Tower of Babylon(DP 最长条件子序列)

     题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于还有一个时  这个长方体能够放在还有一个上面  要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法  比較不好控制 ...

  7. POJ2241——The Tower of Babylon

    The Tower of Babylon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2207   Accepted: 1 ...

  8. UVA437-The Tower of Babylon(动态规划基础)

    Problem UVA437-The Tower of Babylon Accept: 3648  Submit: 12532Time Limit: 3000 mSec Problem Descrip ...

  9. DAG 动态规划 巴比伦塔 B - The Tower of Babylon

    题目:The Tower of Babylon 这是一个DAG 模型,有两种常规解法 1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归 2.递推法 方法一:记忆化搜索 #include < ...

随机推荐

  1. (五)SpringIoc之Bean的作用域

    此文转自 https://blog.csdn.net/icarus_wang/article/details/51586776# 有状态bean和无状态bean请看 https://blog.csdn ...

  2. jQuery实现复选框的全选与全不选

    对于复选框的选中checked属性,实在是无力吐槽. 从上图可以看出,当复选框不设置checked属性时,默认没有被选中:其它三种情况,设置checked属性但不设置属性值即置空,或者将checked ...

  3. oauth 理解

    单点登录 对授权码模式的解读. 1. 用户访问客户端,客户端将请求认证服务器. 2. 用户选择是否给予客户端授权 3.用户授权后,认证服务器将用户导向客户端事先定义好的重定向的地址,同时会附上一个授权 ...

  4. 引入msword

    找到解决方法了:不是直接引入mswork.tlh文件的,该文件是#import "C:\\Program Files\\Microsoft Office\\Office12\\MSWORD. ...

  5. Webstorm 的 Tab 键调整缩进值

    两步即可,注意版本

  6. vue的[__ob__: Observer]

    为什么会获取不到里面的值 因为:vue data 里面值都是有这个属性的.这是被vue接管的数据,observer是Vue核心中最重要的一个模块(个人认为),能够实现视图与数据的响应式更新,底层全凭o ...

  7. IOS开发之关键字synchronized

    static Config * instance = nil; +(Config *) Instance { @synchronized(self)  { if(nil == instance)  { ...

  8. vue slot的使用介绍

    插槽:slot (不知道我这样理解是不是对的,欢迎大佬指点) 具体是什么样子的,请看例子说明 父组件代码 子组件代码 结果 可以看到 ,结果是父组件里面内容显示了,子组件内容显示了,但是在父组件中插入 ...

  9. Chrome插件:本地程序实现验证码破解(浏览器与本地进程通信)

    利用chrome调用本地程序破解图片验证码background.js var port = null, tabId, fname = "vcode.chrome.v1.item.01&quo ...

  10. C#语言中循环分类总结

    C#语言中,循环主要分为4种,分别是:while循环.do while循环.for循环.foeach循环.下面我将分类对循环语句总结. 1.while循环: 如果循环条件为真,则执行循环体:执行完循环 ...