UVA 437_The Tower of Babylon
题意:
一堆石头,给定长宽高,每种石头均可以使用无数次,问这堆石头可以叠放的最高高度,要求下面的石头的长和宽分别严格大于上面石头的长和宽。
分析:
采用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的更多相关文章
- uva The Tower of Babylon[LIS][dp]
转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...
- UVA The Tower of Babylon
The Tower of Babylon Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many det ...
- UVA 437 十九 The Tower of Babylon
The Tower of Babylon Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- UVa 437 The Tower of Babylon(经典动态规划)
传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...
- UVa 437 The Tower of Babylon
Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...
- UVa 437 The Tower of Babylon(DP 最长条件子序列)
题意 给你n种长方体 每种都有无穷个 当一个长方体的长和宽都小于还有一个时 这个长方体能够放在还有一个上面 要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法 比較不好控制 ...
- POJ2241——The Tower of Babylon
The Tower of Babylon Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2207 Accepted: 1 ...
- UVA437-The Tower of Babylon(动态规划基础)
Problem UVA437-The Tower of Babylon Accept: 3648 Submit: 12532Time Limit: 3000 mSec Problem Descrip ...
- DAG 动态规划 巴比伦塔 B - The Tower of Babylon
题目:The Tower of Babylon 这是一个DAG 模型,有两种常规解法 1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归 2.递推法 方法一:记忆化搜索 #include < ...
随机推荐
- 外文翻译 《How we decide》赛场上的四分卫 第三节
本书导言翻译 本章第二节 1982年,一位名叫Elliot的病人走进了神经科学家Antonio Damasio的办公室.几个月之前,一个小的肿瘤在它的大脑中被切除,切除点与大脑额叶非常靠近.在手术之前 ...
- jqueryUI插件
<link rel="stylesheet" href="~/Content/themes/base/jquery-ui.css" /> <s ...
- git --版本对比
比较暂存区域和工作目录 -git diff 分别拷贝暂存区和工作目录的文件到a和b文件夹 --- //表示旧文件 暂存区的 +++ //表示新文件 工作目录的 F 一页一页往下移 B 一 ...
- jmeter 连接 mysql 进行压力测试
- iTOP-4412开发板-实战教程-ssh服务器移植到arm开发板
本文转自迅为开发板:http://www.topeetboard.com 在前面实战教程中,移植了“串口文件传输工具”,整个移植过程是比较简单的,而且我 们没有做任何协议方面的了解,只是“配置”+“编 ...
- iTOP-4418/6818开发板支持双屏异显,双屏同显
iTOP-4418/6818开发板平台安卓系统下支持双屏异显,双屏同显,客户可按照不同用途,分别播放适合屏幕显示方式的内容 ,如HDMI屏幕和LCD屏幕显示不同内容, 一个屏幕播放广告,另一个屏幕运行 ...
- ZooKeeper系列(三)
前面虽然配置了集群模式的Zookeeper,但是为了方面学建议在伪分布式模式的Zookeeper学习Zookeeper的shell命令. 一.Zookeeper的四字命令 Zookeeper支持某些特 ...
- 【转】用jquery编写动态的返回顶部特效
jquery代码: function gotoTop(min_height){ //预定义返回顶部的html代码,它的css样式默认为不显示 var gotoTop_html = '<div i ...
- 03C#数据类型
C#数据类型 值类型和引用类型区别 在C#语言中,值类型变量存储的是指定数据类型的数据,值类型变量的值(或实例)存储在栈(Stack)中,赋值语句是传递变量的值.引用类型(例如类就是引用类型)的实例, ...
- js进行的一些判断
表达式 "^\\d+$" //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$" //正整数 "^((-\\d+)|(0+))$& ...