题意:

一堆石头,给定长宽高,每种石头均可以使用无数次,问这堆石头可以叠放的最高高度,要求下面的石头的长和宽分别严格大于上面石头的长和宽。

分析:

采用DAG最长路算法,由于长宽较大,不能直接用于表示状态,因此采用d[i][x]表示以第i块石头为最高点,以其第x个边为高所能达到的最大高度,其中i为石头标号,x代表长/宽/高,然后根据长宽高要求构造DAG,最后记忆化搜索求出最长路。

代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. using namespace std;
  6. int maxn = 0, ans = 0, n;
  7. int y[35][4];
  8. int G[35][4][35][4];
  9. const int INF = 0x3fffffff;
  10. int a[2],b[2], d[35][4];
  11. int getDAG(int i, int k, int j, int m)
  12. {
  13. if(k==1) a[0] = y[i][2], a[1] = y[i][3];
  14. else if(k==2) a[0] = y[i][3], a[1] = y[i][1];
  15. else a[0] = y[i][2], a[1] = y[i][1];
  16. if(m==1) b[0] = y[j][2], b[1] = y[j][3];
  17. else if(m==2) b[0] = y[j][3], b[1] = y[j][1];
  18. else b[0] = y[j][2], b[1] = y[j][1];
  19. sort(a,a+2);
  20. sort(b,b+2);
  21. return (a[0]>b[0]&&a[1]>b[1])?1:0;
  22. }
  23. int dp(int i, int k)
  24. {
  25. int& ans = d[i][k];
  26. if(ans > 0) return ans;
  27. for(int j = 0; j < n; j++){
  28. for(int m = 1; m <= 3; m++){
  29. if(getDAG(j,m,i,k)) ans = max(ans, dp(j, m));
  30. }
  31. }
  32. ans += y[i][k];
  33. return ans;
  34. }
  35. int main (void)
  36. {
  37. int a, b, c;
  38. cin>>n;
  39. int cnt = 1;
  40. while(n){
  41. maxn = 0;
  42. memset(d,0,sizeof(d));
  43. memset(G,0,sizeof(G));
  44. for(int i = 0; i < n; i++){
  45. cin>>a>>b>>c;
  46. y[i][1] = a; y[i][2] = b; y[i][3] = c;
  47. }
  48. for(int i = 0; i < n; i++)
  49. for(int k = 1; k <= 3; k++)
  50. for(int j = 0; j < n; j++)
  51. for(int m = 1; m <= 3; m++)
  52. getDAG(i,k,j,m);
  53. for(int i = 0; i < n; i++)
  54. for(int k = 1; k <= 3; k++)
  55. maxn = max(maxn,dp(i, k));
  56. cout<<"Case "<<cnt<<": maximum height = "<<maxn<<endl;
  57. cin>>n;
  58. cnt++;
  59. }
  60. }

很白痴的细节问题纠结了一下午,还是要细心!细心!

UVA 437_The Tower of Babylon的更多相关文章

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

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

  2. UVA The Tower of Babylon

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

  3. UVA 437 十九 The Tower of Babylon

    The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

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

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

  5. UVa 437 The Tower of Babylon

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

  6. UVa 437 The Tower of Babylon(DP 最长条件子序列)

     题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于还有一个时  这个长方体能够放在还有一个上面  要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法  比較不好控制 ...

  7. POJ2241——The Tower of Babylon

    The Tower of Babylon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2207   Accepted: 1 ...

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

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

  9. DAG 动态规划 巴比伦塔 B - The Tower of Babylon

    题目:The Tower of Babylon 这是一个DAG 模型,有两种常规解法 1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归 2.递推法 方法一:记忆化搜索 #include < ...

随机推荐

  1. [BZOJ1016][JSOI2008]最小生成树计数 最小生成树 搜索

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1016 做这道题之前需要知道一些结论,同一个图的最小生成树中相同权值的边的个数是不会变的,如 ...

  2. php(三)使用PDO链接数据库

    1.启动 mysql数据库,打开图形化控制界面 2.新建一个数据库 3.创建一个数据表 4.给数据表添加数据 id是数字类型的  类型选择int长度 11 username 等其他数据  会是字符串形 ...

  3. 【译】x86程序员手册41-10.6 TLB(快表)测试

    译注:本章基本未做翻译 10.6 TLB Testing TLB测试 The 80386 provides a mechanism for testing the Translation Lookas ...

  4. windows sdk编程禁止窗体最大化最小化

    #include <windows.h> /*消息处理函数声明*/ HRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM ...

  5. 哈尔滨工程大学ACM预热赛 补题

    链接:https://ac.nowcoder.com/acm/contest/554/A来源:牛客网 小虎刚刚上了幼儿园,老师让他做一个家庭作业:首先画3个格子,第二行有2个格子,第三行有1个格子. ...

  6. 单页vue路由router

    Vue.js + vue-router 可以很简单的实现单页应用. <router-link> 是一个组件,该组件用于设置一个导航链接,切换不同 HTML 内容. to 属性为目标地址, ...

  7. 07JavaScript数组与字符串对象

    JavaScript数组与字符串对象 5.1.1数组(Array)对象 <script> //声明一个数组并赋值; var arr = new Array("aa",& ...

  8. wdcp 打开网页显示 Apache 2 Test Page powered by CentOS -- 来自辉哥博客

    是因为更新过系统,安装并更新了系统自带的apache 执行这个命令即可 #ln -sf /www/wdlinux/init.d/httpd /etc/rc.d/init.d/httpd#reboot ...

  9. 【原】CentosDocker安装(一)

    CentosDocker安装 来源:https://www.runoob.com/docker/centos-docker-install.html 1.前提条件 目前,CentOS 仅发行版本中的内 ...

  10. Chrome插件:微信公众号自动登录(chrome.extension)

    manifest.json: { "manifest_version": 2, "name": "WX.AutoLogin", " ...