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 < ...
随机推荐
- JS filters-table简单过滤-Version 1
初级版本: 输入框可检索,空格检索忽略 radio点击后会过滤 最后的两个输入框可根据age范围检索过滤 三个单独的功能,不能协同工作 <!DOCTYPE html> <html&g ...
- CentOS 7 下用 firewall-cmd / iptables 实现 NAT 转发供内网服务器联网
自从用 HAProxy 对服务器做了负载均衡以后,感觉后端服务器真的没必要再配置并占用公网IP资源. 而且由于托管服务器的公网 IP 资源是固定的,想上 Keepalived 的话,需要挤出来 3 个 ...
- idea 调试工具的使用
原文:https://blog.csdn.net/hao_hl1314/article/details/53120918 Intellij IDEA Debug调试区工具的使用方法 快捷键F9 ...
- 【C++】模板简述(五):类型萃取
功能 类型萃取,在STL中用到的比较多,用于判断一个变量是否为POD类型. 简述来说可以用来判断出某个变量是内置类型还是自定义类型. 通过类型萃取,萃取到变量类型,对不同变量进行不同处理,可以提升程序 ...
- 1、QQ装机部落---腾讯软件
亲,赚钱快人一步,我给你推荐QQ装机部落!大品牌,口碑好,知名软件,绿色无毒,每天结算,回报高!到这里注册:http://zjbl.qq.com/#/?invid=476058088 返利网邀请码立即 ...
- 转:谈谈iOS中粘性动画以及果冻效果的实现
在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: 先做个提纲: 第一个分享的主题是 ...
- js 复制文字、 复制链接到粘贴板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java IO(四--字符流基本使用
在上一节,介绍了字节流的基本使用,本节介绍一下字符流的使用 Reader: public abstract class Reader implements Readable, Closeable { ...
- JS中二进制与十进制的相互转换
今天在做题目的时候遇到了需要十进制转换为二进制,这个我知道用toString可以,但是二进制转换为十进制我一下子就想不起来,网上搜了下,才知道是parseInt可以实现,特此记录下. 十进制转换为二进 ...
- Filter简介
Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 htm ...