http://wenku.baidu.com/view/728cd5126edb6f1aff001fbb.html 关于悬线法,这里面有详解。

我当时只想到了记录最大长度,却没有想到如果连最左边和最右边的位置都记录的话这题就可以解决了。 学习了一种新算法很开心。

Cut the cake

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 881    Accepted Submission(s): 328

Problem Description
Mark bought a huge cake, because his friend ray_sun’s birthday is coming. Mark is worried about how to divide the cake since it’s so huge and ray_sun is so strange. Ray_sun is a nut, you can never imagine how strange he was, is, and going to be. He does not eat rice, moves like a cat, sleeps during work and plays games when the rest of the world are sleeping……It is not a surprise when he has some special requirements for the cake. A considering guy as Mark is, he will never let ray_sun down. However, he does have trouble fulfilling ray_sun’s wish this time; could you please give him a hand by solving the following problem for him?
  The cake can be divided into n*m blocks. Each block is colored either in blue or red. Ray_sun will only eat a piece (consisting of several blocks) with special shape and color. First, the shape of the piece should be a rectangle. Second, the color of blocks in the piece should be the same or red-and-blue crisscross. The so called ‘red-and-blue crisscross’ is demonstrated in the following picture. Could you please help Mark to find out the piece with maximum perimeter that satisfies ray_sun’s requirements?
 
Input
The first line contains a single integer T (T <= 20), the number of test cases.
  For each case, there are two given integers, n, m, (1 <= n, m <= 1000) denoting the dimension of the cake. Following the two integers, there is a n*m matrix where character B stands for blue, R red.
 
Output
For each test case, output the cased number in a format stated below, followed by the maximum perimeter you can find.
 
Sample Input
2
1 1
B
3 3
BBR
RBB
BBB
 
Sample Output
Case #1: 4
Case #2: 8
 
Author
BJTU
 
Source
 
Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:  4321 4366 4335 4377 4371 
 
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <algorithm>
  5. using namespace std;
  6. #define N 1010
  7.  
  8. //最大权子矩阵
  9. int n,m;
  10. int dp[N][N]; //记录每个点可以往上到达的最远距离
  11. int dpl[N][N]; //记录这个点往左最远走多远
  12. int dpr[N][N]; //
  13. char g[N][N];
  14. int ans=;
  15. int tmpl[N],tmpr[N];
  16.  
  17. void fuc(char key)
  18. {
  19. memset(dp,,sizeof(dp));
  20. memset(dpl,,sizeof(dpl));
  21. memset(dpr,,sizeof(dpr));
  22. int mx=;
  23. for(int i=;i<=m;i++)
  24. {
  25. dpl[][i]=;
  26. dpr[][i]=m;
  27. }
  28.  
  29. for(int i=;i<=n;i++)
  30. {
  31. int tmp=;
  32. for(int j=;j<=m;j++)
  33. {
  34. if(g[i][j]!=key)
  35. {
  36. tmp=j+;
  37. }
  38. else
  39. {
  40. tmpl[j]=tmp;
  41. }
  42. }
  43. tmp=m;
  44. for(int j=m;j>=;j--)
  45. {
  46. if(g[i][j]!=key)
  47. {
  48. tmp=j-;
  49. }
  50. else tmpr[j]=tmp;
  51. }
  52. //记录好了一个点能到达的最左边和最右边,接下来就是dp了
  53. for(int j=;j<=m;j++)
  54. {
  55. if(g[i][j]!=key)
  56. {
  57. dp[i][j]=;//最长为0
  58. dpl[i][j]=; dpr[i][j]=m;
  59. continue;
  60. }
  61. dp[i][j]=dp[i-][j]+;
  62. dpl[i][j]=max(dpl[i-][j],tmpl[j]);
  63. dpr[i][j]=min(dpr[i-][j],tmpr[j]);
  64. mx=max(*(dpr[i][j]-dpl[i][j]++dp[i][j]),mx);
  65. }
  66. }
  67. ans=max(ans,mx);
  68. }
  69.  
  70. int main()
  71. {
  72. int T;
  73. int tt=;
  74. scanf("%d",&T);
  75. while(T--)
  76. {
  77. ans=;
  78. scanf("%d%d",&n,&m);
  79. for(int i=;i<=n;i++)
  80. scanf("%s",g[i]+);
  81. fuc('B');
  82. fuc('R');
  83. for(int i=;i<=n;i++)
  84. for(int j=;j<=m;j++)
  85. {
  86. if( (i+j)%== )
  87. {
  88. if(g[i][j]=='B') g[i][j]='R';
  89. else g[i][j]='B';
  90. }
  91. }
  92. //想的还是比较清晰的...
  93.  
  94. fuc('B');
  95. fuc('R');
  96. printf("Case #%d: ",tt++);
  97. printf("%d\n",ans);
  98. }
  99. return ;
  100. }

hdu4328(经典dp用悬线法求最大子矩形)的更多相关文章

  1. BZOJ 1057: [ZJOI2007]棋盘制作 悬线法求最大子矩阵+dp

    1057: [ZJOI2007]棋盘制作 Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑 ...

  2. City Game UVALive - 3029(悬线法求最大子矩阵)

    题意:多组数据(国外题好像都这样),每次n*m矩形,F表示空地,R表示障碍 求最大子矩阵(悬线法模板) 把每个格子向上延伸的空格看做一条悬线 以le[i][j],re[i][j],up[i][j]分别 ...

  3. [DP专题]悬线法

    参考:https://blog.csdn.net/twtsa/article/details/8120269 先给出题目来源:(洛谷) 1.p1387 最大正方形 2.P1169 棋盘制作 3.p27 ...

  4. P4147 玉蟾宫(悬线法求最大子矩阵)

    P4147 玉蟾宫 悬线法 ,\(l_{i,j},r_{i,j},up_{i,j}\) 分别表示 \((i,j)\) 这个点向左,右,上能到达的远点.然后面积就很好办了.具体实现见代码. 然而,还有更 ...

  5. bzoj 3039: 玉蟾宫 单调栈或者悬线法求最大子矩阵和

    3039: 玉蟾宫 Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 有一天,小猫rainbow ...

  6. bzoj 3039 悬线法求最大01子矩阵

    首先预处理每个F点左右,下一共有多少个F点,然后 对于每个为0的点(R),从这个点开始,一直到这个点 下面第一个R点,这一区间中的min(左),min(右)更新答案. ps:我估计这道题数据有的格式不 ...

  7. 【BZOJ-3039&1057】玉蟾宫&棋盘制作 悬线法

    3039: 玉蟾宫 Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 753  Solved: 444[Submit][Status][Discuss] D ...

  8. 2018.09.29 bzoj3885: Cow Rectangles(悬线法+二分)

    传送门 对于第一个问题,直接用悬线法求出最大的子矩阵面积,然后对于每一个能得到最大面积的矩阵,我们用二分法去掉四周的空白部分来更新第二个答案. 代码: #include<bits/stdc++. ...

  9. [ZJOI2007]棋盘制作 悬线法dp 求限制下的最大子矩阵

    https://www.luogu.org/problemnew/show/P1169 第一次听说到这种dp的名称叫做悬线法,听起来好厉害 题意是求一个矩阵内的最大01交错子矩阵,开始想的是dp[20 ...

随机推荐

  1. 最接近WeChat的全屏自定义相机(Custom Camera)

    代码地址如下:http://www.demodashi.com/demo/13271.html 一.需求 最接近WeChat的全屏自定义相机(Custom Camera),拍照和预览都是全屏尺寸.使用 ...

  2. mongodb的基本语法(二)

    一.聚集集合查询 1.查询所有记录 db.userInfo.find(); 相当于:select* from userInfo; 默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一 ...

  3. HTML-HTML5+CSS3权威指南阅读(三、CSS3)

    不同的浏览器(包括-moz-代表的Mozilla Firefox, -ms-代表的Microsoft Internet Explorer等)厂商在发布正式版本之前之前, 试验各自对CSS3新特性的实现 ...

  4. font-size<12 chrome不支持解决

    今天,群里有人问道font-size<12 chrome不支持的问题.说实话,这个我一直都没留意过,正好借个机会给自己补习一下. 自己亲自试过,确实如此,当font-size<12 chr ...

  5. Ant—使用Ant构建简单Java项目(三)

    博客<Ant-使用Ant构建简单Java项目(二)>我们简化了运行Test类中main方法须要运行的命令,本博客来介绍一下如何使build.xml文件和当中使用property标签定义的属 ...

  6. git diff 文件对比

    1.  git diff  filepath 工作区与暂存区比较 2. git diff HEAD filepath 工作区与HEAD ( 当前工作分支) 比较 3. git diff --stage ...

  7. SVN服务器更改ip地址客户端怎么设置(转载)

    SVN 服务器 IP 地址修改后,客户端对服务器的连接可以采用以下的方法重定位: 1. 如果客户端工具是TortoiseSVN,直接在工作副本上右键,选择TortoiseSVN->relocat ...

  8. ((void *) 0)的含义和void的一些细节

    一.在c语言中,0是一个特殊的值,它可以表示:整型数值0,空字符,逻辑假(false).表示的东西多了,有时候不好判断.尤其是空字符和数字0之间. 为了明确的指出,0是空字符的含义,用用到了: (() ...

  9. 解决Ubuntu下博通网卡驱动问题

    sudo apt-get install bcmwl-kernel-source我的无线网卡是BCM4312 802.11b/g 在12.04 会集成驱动,装完就能用,现在用14.04,16.04里面 ...

  10. atitit.软件设计模式大的总结attialx总结

    atitit.软件设计模式大的总结attialx总结 1. 设计模式的历史3 2. 设计模式的数量(253个)3 3. 设计模式的结构4 3.1. 应用场景and条件Context4 3.2. Pro ...