题目: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. kafka集群partition分布原理分析

    1. Kafka集群partition replication默认自动分配分析 下面以一个Kafka集群中4个Broker举例,创建1个topic包含4个Partition,2 Replication ...

  2. php使用memcached缓存总结

    1. 查询多行记录,以sql的md5值为key,缓存数组(个人觉得最好用的方法) $mem = new Memcache(); $mem->connect('127.0.0.1',11211); ...

  3. Python机器学习笔记:深入理解Keras中序贯模型和函数模型

     先从sklearn说起吧,如果学习了sklearn的话,那么学习Keras相对来说比较容易.为什么这样说呢? 我们首先比较一下sklearn的机器学习大致使用流程和Keras的大致使用流程: skl ...

  4. linux图形化客户端

    很多服务器都用linux 但这些linux都是没有图形化界面的, 一般也不建议在服务器上装图形化界面 我们都知道,维护linux,大部分都是使用命令 那么,为什么不能开发一个应用程序, 把图形化操作转 ...

  5. Shell 数组定义与获取

    Shell 数组 bash支持一维数组(不支持多维数组),并且没有限定数组的大小. 类似与 C 语言,数组元素的下标由 0 开始编号.获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于 ...

  6. 一个mui扩展插件mui.showLoading加载框【转】

    转:http://ask.dcloud.net.cn/article/12856 写在前面:好像mui目前dialog系列唯独缺少showLoading加载框(加载中)组件,为了统一组件样式和体验,写 ...

  7. mysql查询语句,通过limit来限制查询的行数。

    mysql查询语句,通过limit来限制查询的行数. 例如: select name from usertb where age > 20 limit 0, 1; //限制从第一条开始,显示1条 ...

  8. 性能监控(3)–linux下的iostat命令

    iostat可以显示cpu与磁盘信息,添加-d参数可以只显示磁盘信息

  9. What is The Rule of Three?

    Question: What does copying an object mean? What are the copy constructor and the copy assignment op ...

  10. blfs(systemv版本)学习笔记-配置远程连接显示中文

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 远程连接的lfs系统需要具备以下环境便可在xshell或其他远程终端上面显示中文: 1.lfs主机设置中文编码(需要配置) 2. ...