题目:The Tower of Babylon

这是一个DAG 模型,有两种常规解法

1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归

2.递推法

方法一:记忆化搜索
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int x,y,z;
node(int x=0,int y=0,int z=0) : x(x) , y(y) , z(z){}
}exa[5000]; bool cmp(node a,node b)
{
if(a.x==b.x) return a.y>b.y;
else return a.x>b.x;
} int n;
int dp[10000];
bool vis[10000]; int d(int e)
{
if(vis[e]) return dp[e];
vis[e]=1;
int &ans=dp[e];
ans=exa[e].z;
for(int i=1;i<e;i++)
{
// printf("exa[%d].x=%d\n",i,exa[i].x);
// printf("exa[%d].y=%d\n",i,exa[i].y);
// printf("exa[%d].x=%d\n",e,exa[e].x);
// printf("exa[%d].y=%d\n",e,exa[e].y);
if(exa[i].x>exa[e].x&&exa[i].y>exa[e].y)
{
ans=max(ans,d(i)+exa[e].z);
// cout<<ans<<" "<<i<<endl;
}
}
return ans;
} int main()
{
int num=0;
while(scanf("%d",&n)==1&&n)
{
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
exa[i]=node(a,b,c);
exa[n+i]=node(b,c,a);
exa[n*2+i]=node(b,a,c);
exa[n*3+i]=node(a,c,b);
exa[n*4+i]=node(c,a,b);
exa[n*5+i]=node(c,b,a);
}
n*=6;
sort(exa+1,exa+n+1,cmp);
int ans=0;
for(int i=1;i<=n;i++)
{
int tmp=d(i);
ans=max(ans,tmp);
}
printf("Case %d: maximum height = %d\n",++num,ans);
}
return 0;
}

  

方法二:递推法
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int x,y,z;
node(int x=,int y=,int z=) : x(x) , y(y) , z(z){}
}exa[];
bool cmp(node a,node b)
{
if(a.x==b.x) return a.y>b.y;
else return a.x>b.x;
} int n;
int dp[]; int main()
{
int num=;
while(scanf("%d",&n)==&&n)
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
exa[i]=node(a,b,c);
exa[n+i]=node(b,c,a);
exa[n*+i]=node(b,a,c);
exa[n*+i]=node(a,c,b);
exa[n*+i]=node(c,a,b);
exa[n*+i]=node(c,b,a);
}
n*=;
int ans=;
sort(exa+,exa+n+,cmp);
for(int i=;i<=n;i++)
{
dp[i]=exa[i].z;
for(int j=;j<i;j++)
{
if(exa[i].x<exa[j].x&&exa[i].y<exa[j].y)
{
dp[i]=max(dp[i],dp[j]+exa[i].z);
//cout<<dp[i]<<"ww "<<i<<endl;
}//cout<<dp[i]<<" "<<i<<endl;
}
ans=max(ans,dp[i]);
}
printf("Case %d: maximum height = ",++num);
cout<<ans<<endl;
}
return ;
}

DAG 动态规划 巴比伦塔 B - The Tower of Babylon的更多相关文章

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

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

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

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

  3. [动态规划]UVA437 - The Tower of Babylon

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

  4. ACM - 动态规划 - UVA437 The Tower of Babylon

    UVA437 The Tower of Babylon 题解 初始时给了 \(n\) 种长方体方块,每种有无限个,对于每一个方块,我们可以选择一面作为底.然后用这些方块尽可能高地堆叠成一个塔,要求只有 ...

  5. UVA The Tower of Babylon

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

  6. UVa 437 The Tower of Babylon

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

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

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

  8. POJ 2241 The Tower of Babylon

    The Tower of Babylon Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Or ...

  9. Uva437 The Tower of Babylon

    https://odzkskevi.qnssl.com/5e1fdf8cae5d11a8f572bae96d6095c0?v=1507521965 Perhaps you have heard of ...

随机推荐

  1. HTML XML 介绍

    一. HTML(HyperTextMark-upLanguage)即超文本标记语言,是WWW的描述语言. 二. XML即ExtentsibleMarkup Language(可扩展标记语言), XML ...

  2. [转] can not find module @angular/animations/browser

    本文转自:https://blog.csdn.net/yaerfeng/article/details/68956298 angularjs4升级了,原来的animations现在被单独出来一个包. ...

  3. maven详细配置

    Eclipse上Maven环境配置使用 (全) 1. 安装配置Maven: 1.1 从Apache网站 http://maven.apache.org/ 下载并且解压缩安装Apache Maven. ...

  4. Matlab function lorenzgui

    function lorenzgui %LORENZGUI Plot the orbit around the Lorenz chaotic attractor. % This function an ...

  5. Python全栈学习_day002知识点

    今日大纲: . while循环 . 格式化输出 . 运算符 . 编码初识 1. while循环 - while 无限循环: while True: # 死循环 print('大悲咒') print(' ...

  6. canvas-color的几种设置

    #ffff #642 = #664422 rgb(255,128,0) rgba(100,100,100,0.8) hsl(20,62%,28%) hsla(40,82%,33%,0.6) red

  7. addEventListener.js

    document.addEventListener("click",function(){ console.log("添加事件监听") }) 举个例子 : 点击 ...

  8. git与vscode连接的一种简单方式

    首先你得安装git,如果你还没安装git,推荐你一个视频git的下载与安装,这套视频包含VS code和git的基本使用,当你看完它,就可以不看我的这篇博客了. 废话不多说,直接进入正题: 首先进入g ...

  9. 修改 this 指向

    封装函数 f,使 f 的 this 指向指定的对象 function bindThis(f, oTarget) { if(f.bind){ return f.bind(oTarget); }else{ ...

  10. Salesforce中的单点登录简介

    单点登录的定义 引自维基百科: 单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自独立的软件系统,提供访问控制的属性.当拥有这项属性时, ...