# [HDU 4328] Cut the cake （悬链法）

### HDU - 4328

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))

const int maxn=1e3+10;
int N,M;
char Map[maxn][maxn];
int L[3][maxn][maxn], R[3][maxn][maxn];
int H[3][maxn][maxn];

int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
#endif

int T;
scanf("%d", &T);
for(int ck=1; ck<=T; ck++)
{
CLR(L); CLR(R); CLR(H);
CLR(Map);
int ans=4;
scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++)
{
scanf(" %s", Map[i]+1);
for(int j=1; j<=M; j++)
{
L[0][i][j] = L[0][i][j-1] + 1;
if( Map[i][j] != 'R' ) L[0][i][j] = 0;

L[1][i][j] = L[1][i][j-1] + 1;
if( Map[i][j] != 'B' ) L[1][i][j] = 0;

L[2][i][j] = L[2][i][j-1] + 1;
if( Map[i][j] == Map[i][j-1]) L[2][i][j] = 1;
}
for(int j=M; j>=1; j--)
{
R[0][i][j] = R[0][i][j+1] + 1;
if( Map[i][j] != 'R' ) R[0][i][j] = 0;

R[1][i][j] = R[1][i][j+1] + 1;
if( Map[i][j] != 'B' ) R[1][i][j] = 0;

R[2][i][j] = R[2][i][j+1] + 1;
if( Map[i][j] == Map[i][j+1]) R[2][i][j] = 1;
}
for(int j=1; j<=M; j++)
{
H[0][i][j] = H[0][i-1][j] + 1;
H[1][i][j] = H[1][i-1][j] + 1;
H[2][i][j] = H[2][i-1][j] + 1;

if( Map[i][j] != 'R' ) H[0][i][j] = 0;
if( Map[i][j] != 'B' ) H[1][i][j] = 0;
if( Map[i-1][j] == Map[i][j]) H[2][i][j] = 1;

if( Map[i-1][j] == 'R' ) L[0][i][j] = min( L[0][i][j], L[0][i-1][j]);
if( Map[i-1][j] == 'B' ) L[1][i][j] = min( L[1][i][j], L[1][i-1][j]);
if( i>1 && Map[i-1][j] != Map[i][j] ) L[2][i][j] = min( L[2][i][j], L[2][i-1][j]);

if( Map[i-1][j] == 'R' ) R[0][i][j] = min( R[0][i][j], R[0][i-1][j]);
if( Map[i-1][j] == 'B' ) R[1][i][j] = min( R[1][i][j], R[1][i-1][j]);
if( i>1 && Map[i-1][j] != Map[i][j] ) R[2][i][j] = min( R[2][i][j], R[2][i-1][j]);

ans = max(ans, 2*(H[0][i][j] + L[0][i][j]+R[0][i][j]-1 ));
ans = max(ans, 2*(H[1][i][j] + L[1][i][j]+R[1][i][j]-1 ));
ans = max(ans, 2*(H[2][i][j] + L[2][i][j]+R[2][i][j]-1 ));

//              printf("i:%d j:%d H:%d L:%d R:%d\n", i, j, H[1][i][j], L[1][i][j], R[1][i][j]);
}
}

printf("Case #%d: %d\n", ck, ans);
}
return 0;
}


#### HDU 4328 - Cut the cake

2012-07-31 19:27:04

#### hdu 4328 Cut the cake

2013-10-18 16:43:06

#### HDU 4328 Cut the cake

2017-03-17 16:22:35

#### hdu 4328 Cut the cake 最大子矩阵

2012-08-11 15:09:38

#### hdu - 4328 - Cut the cake - dp

2012-10-22 17:06:56

#### HDU 4328 Cut the Cake（动态规划）

2012-08-02 19:19:30

#### Cut The Cake

2017-04-04 18:46:15

#### HDU 4328 Cut the cake（最大子矩阵+悬线法）

2015-11-09 19:00:56

#### HDU cake1722

2016-05-05 02:29:36

#### Hdu 2513 Cake slicing【区间Dp】

2017-06-22 19:16:02