HDU 4328 Cut the cake

Cut the cake

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


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
2012 Multi-University Training Contest 3
Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:  4321 4366 4335 4377 4371 
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 #include<algorithm>
 6 #define N 1010
 7 int n,m,h[N][N],l[N][N],r[N][N];
 8 char g[N][N];
 9 int ans=0,tmpl[N],tmpr[N];
10 void solve(char key){
11     memset(h,0,sizeof(h));
12     memset(l,0,sizeof(l));
13     memset(r,0,sizeof(r));
14     int mx=0;
15     for(int i=1;i<=m;i++){
16         l[0][i]=0;r[0][i]=m;
17     }
18     for(int i=1;i<=n;i++){
19         int tmp=1;
20         for(int j=1;j<=m;j++){
21             if(g[i][j]!=key)tmp=j+1;
22             else tmpl[j]=tmp;
23         }
24         tmp=m;
25         for(int j=m;j>=1;j--){
26             if(g[i][j]!=key) tmp=j-1;
27             else tmpr[j]=tmp;
28         }
29         //记录好了一个点能到达的 最左边 和最右边,
30         //接下来就是dp了
31         for(int j=1;j<=m;j++){
32             if(g[i][j]!=key){
33                 l[i][j]=1;h[i][j]=0;r[i][j]=m;
34                 continue;
35             }
36             h[i][j]=h[i-1][j]+1;
37             l[i][j]=max(l[i-1][j],tmpl[j]);
38             r[i][j]=min(r[i-1][j],tmpr[j]);
39             mx=max(2*(r[i][j]-l[i][j]+1+h[i][j]),mx);
40         }
41     }
42     ans=max(ans,mx);
43 }
44 int main()
45 {
46     int T,tt=1;scanf("%d",&T);
47     for(int tz=1;tz<=T;tz++){
48         ans=0;
49         scanf("%d%d",&n,&m);
50         for(int i=1;i<=n;i++)
51           scanf("%s",g[i]+1);
52         solve('B');solve('R');
53         for(int i=1;i<=n;i++)
54           for(int j=1;j<=m;j++)
55             if((i+j)%2==0){
56               if(g[i][j]=='B')g[i][j]='R';
57               else g[i][j]='B';
58             }
59         solve('B');solve('R');
60         printf("Case #%d: ",tt++);
61         printf("%d\n",ans);
62     }
63     return 0;
64 }

思路:悬线法~~~~DP求满足题意的最大子矩阵的边长

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源中直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值