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 
 
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define N 1010 //最大权子矩阵
int n,m;
int dp[N][N]; //记录每个点可以往上到达的最远距离
int dpl[N][N]; //记录这个点往左最远走多远
int dpr[N][N]; //
char g[N][N];
int ans=;
int tmpl[N],tmpr[N]; void fuc(char key)
{
memset(dp,,sizeof(dp));
memset(dpl,,sizeof(dpl));
memset(dpr,,sizeof(dpr));
int mx=;
for(int i=;i<=m;i++)
{
dpl[][i]=;
dpr[][i]=m;
} for(int i=;i<=n;i++)
{
int tmp=;
for(int j=;j<=m;j++)
{
if(g[i][j]!=key)
{
tmp=j+;
}
else
{
tmpl[j]=tmp;
}
}
tmp=m;
for(int j=m;j>=;j--)
{
if(g[i][j]!=key)
{
tmp=j-;
}
else tmpr[j]=tmp;
}
//记录好了一个点能到达的最左边和最右边,接下来就是dp了
for(int j=;j<=m;j++)
{
if(g[i][j]!=key)
{
dp[i][j]=;//最长为0
dpl[i][j]=; dpr[i][j]=m;
continue;
}
dp[i][j]=dp[i-][j]+;
dpl[i][j]=max(dpl[i-][j],tmpl[j]);
dpr[i][j]=min(dpr[i-][j],tmpr[j]);
mx=max(*(dpr[i][j]-dpl[i][j]++dp[i][j]),mx);
}
}
ans=max(ans,mx);
} int main()
{
int T;
int tt=;
scanf("%d",&T);
while(T--)
{
ans=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",g[i]+);
fuc('B');
fuc('R');
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if( (i+j)%== )
{
if(g[i][j]=='B') g[i][j]='R';
else g[i][j]='B';
}
}
//想的还是比较清晰的... fuc('B');
fuc('R');
printf("Case #%d: ",tt++);
printf("%d\n",ans);
}
return ;
}

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. 自制MVC框架的插件与拦截器基础

    上篇谈到我自己写的MVC框架,接下来讲讲插件及拦截器! 在处理一些通用的逻辑最好把它封装一个插件或者拦截器,以便日后可以直接拿过来直接使用.在我的框架中可以通过继承以下抽象类来实现插件或者拦截器. 1 ...

  2. C# Interview Questions:C#-English Questions

    This is a list of questions I have gathered from other sources and created myself over a period of t ...

  3. vue的组件(先学习其他的ES6知识,之后再看这个) (未完)

    https://blog.csdn.net/qq20004604/article/details/56965703

  4. 代理模式和php实现

    代理模式(Proxy Pattern) : 给某一个对象提供一个代 理,并由代理对象控制对原对象的引用.代理模式的英 文叫做Proxy或Surrogate,它是一种对象结构型模式 模式动机: 在某些情 ...

  5. windows server 2003中端口默认不能使用问题

    问题:在windows server 2003中IIS6.0新建站点,给了一个新端口(非80),然后配置好后不能访问 解决方案:系统内置防火墙需要添加对应端口,如下图:     即解决.

  6. atitit. 分销系统规划p8k

    atitit. 分销系统规划p8k 1. 商户平台管理 overview2 1.1. 分销业务管理2 1.2. 文案管理2 1.3. 订单管理3 1.4. 统计报表3 1.5. 财务结算3 1.6.  ...

  7. python3 解析xml

    转载:http://www.jb51.net/article/79494.htm 这篇文章主要为大家详细介绍了深入解读Python解析XML的几种方式,以ElementTree模块为例,演示具体使用方 ...

  8. tensorflow 之模型的保存与加载(二)

    上一遍博文提到 有些场景下,可能只需要保存或加载部分变量,并不是所有隐藏层的参数都需要重新训练. 在实例化tf.train.Saver对象时,可以提供一个列表或字典来指定需要保存或加载的变量. #!/ ...

  9. 0055 <mvc:default-servlet-handler />不能同时访问静态资源和动态资源的问题

    刚才在折腾的时候,发现applicationContext.xml中配置了<mvc:default-servlet-handler />后,能访问静态资源,却不能访问动态资源了:注释掉之后 ...

  10. 基于Java Mina框架的部标jt808服务器设计和开发

    在开发部标GPS平台中,部标jt808GPS服务器是系统的核心关键,决定了部标平台的稳定性和行那个.Linux服务器是首选,为了跨平台,开发语言选择Java自不待言.需要购买jt808GPS服务器源码 ...