题目: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. 类Exception_A继承Exception,类Exception_B继承Exception_A,请问执行此段代码的输出是什么?

    @Test public void Test_Exception() { try { throw new ExceptionB("A"); } catch (ExceptionA ...

  2. jQuery基础教程

    1.使用$()函数 $()函数其实是创建了一个jQuery对象. 这个函数接受CSS选择符作为参数,充当一个工厂, 返回包含页面中对应元素的jQuery对象. 所有能在样式表中使用的选择符都可以传给这 ...

  3. Nuget快捷实践

    Nuget快捷实践 简介 本文主要包含三个部分,即创建nuget包.上传nuget包和删除nuget包.旨在引导快速使用nuget打包和使用,并实现对于nuget的简单管理. 本文使用的nuget服务 ...

  4. C# MVC 用户登录状态判断

    来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类AuthenticationAttribute ,代码如下: ...

  5. C++ enum的使用

    enum day {Sun,Mon,Tue,Wed,Thu,Fri,Sat};  默认情况下,枚举符的值从0开始,其后值总是前面一个+1.  即Sun=0,Mon=1,Tue=2,Wed=3,Thu= ...

  6. JS日期Date详解与实例扩展

    一:Date类型介绍 要创建一个日期对象,使用new操作符和Date构造函数即可: var now = new Date(); Date.parse()方法 其中Date.parse()方法接收一个表 ...

  7. js 复制文本到粘贴板

    //html 在iOS Safari中,剪贴板API有一些限制(实际上是安全措施):   于安全原因,iOS Safari只允许容器中的document.execCommand('copy')文本co ...

  8. JSP内置对象——Exception对象

    举个实例说明下: 新建一个“exception_test.jsp”: 对应的exception.jsp页面: 运行“exception_test.jsp”后: 虽然执行的是“exception_tes ...

  9. loadrunner 运行脚本-Run-time Settings-Browser Enmulation设置详解

    运行脚本-Run-time Settings-Browser Enmulation设置详解 by:授客 QQ:1033553122 浏览器模拟 所有Internet Vuser Header包含一个标 ...

  10. loadrunner 运行场景-Controller及Load Generators宿主主机优化

    运行场景-Controller及Load Generators宿主主机优化 by:授客 QQ:1033553122 1.  Load Generator宿主主机优化设置 win7以下:进入“控制面板” ...