悬线法可解,稍有点烦琐。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string.h>
using namespace std; char map[1010][1010];
int lefts[1010][1010],heights[1010][1010],rights[1010][1010]; int CountSingle(int n,int m,char sing){
int ret=0;
for(int i=1;i<=n;i++){
if(i==1){
int l=0;
for(int j=1;j<=m;j++){
if(map[i][j]!=sing){
heights[i][j]=0;
lefts[i][j]=m+1;
l=j;
}
else{
heights[i][j]=1;
lefts[i][j]=l+1;
}
}
int r=m+1;
for(int j=m;j>=1;j--){
if(map[i][j]!=sing){
rights[i][j]=0;
r=j;
}
else{
rights[i][j]=r-1;
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
}
}
else{
int l=0;
for(int j=1;j<=m;j++){
if(map[i][j]!=sing){
heights[i][j]=0;
lefts[i][j]=m+1;
l=j;
}
else{
if(map[i-1][j]==sing){
heights[i][j]=heights[i-1][j]+1;
lefts[i][j]=max(lefts[i-1][j],l+1);
}
else{
heights[i][j]=1;
lefts[i][j]=l+1;
}
}
}
int r=m+1;
for(int j=m;j>=1;j--){
if(map[i][j]!=sing){
rights[i][j]=0;
r=j;
}
else{
if(map[i-1][j]==sing){
rights[i][j]=min(rights[i-1][j],r-1);
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
else{
rights[i][j]=r-1;
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
}
}
}
}
return ret;
} int CountCross(int n,int m){
int ret=0;
for(int i=1;i<=n;i++){
if(i==1){
int l=0;
for(int j=1;j<=m;j++){
if(j==1){
heights[i][j]=1;
lefts[i][j]=1;
}
else{
if(map[i][j-1]==map[i][j]){
lefts[i][j]=j;
l=j-1;
heights[i][j]=1;
}
else{
lefts[i][j]=l+1;
heights[i][j]=1;
}
}
}
int r=m+1;
for(int j=m;j>=1;j--){
if(j==m){
heights[i][j]=1;
rights[i][j]=m;
}
else{
if(map[i][j]==map[i][j+1]){
heights[i][j]=1;
rights[i][j]=j;
r=j+1;
}
else{
rights[i][j]=r-1;
heights[i][j]=1;
}
}
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
}
else{
int l=0;
for(int j=1;j<=m;j++){
if(j==1){
if(map[i][j]!=map[i-1][j]){
heights[i][j]=heights[i-1][j]+1;
lefts[i][j]=1;
}
else{
heights[i][j]=1;
lefts[i][j]=1;
}
}
else{
if(map[i][j]!=map[i-1][j]){
heights[i][j]=heights[i-1][j]+1;
if(map[i][j]==map[i][j-1]){
lefts[i][j]=j;
l=j-1;
}
else{
lefts[i][j]=max(lefts[i-1][j],l+1);
}
}
else{
heights[i][j]=1;
if(map[i][j]==map[i][j-1]){
lefts[i][j]=j;
l=j-1;
}
else{
lefts[i][j]=l+1;
}
}
}
}
int r=m+1;
for(int j=m;j>=1;j--){
if(j==m){
if(map[i][j]!=map[i-1][j]){
heights[i][j]=heights[i-1][j]+1;
rights[i][j]=m;
}
else{
heights[i][j]=1;
rights[i][j]=m;
}
}
else{
if(map[i][j]!=map[i-1][j]){
heights[i][j]=heights[i-1][j]+1;
if(map[i][j]==map[i][j+1]){
rights[i][j]=j;
r=j+1;
}
else{
rights[i][j]=min(rights[i-1][j],r-1);
}
}
else{
heights[i][j]=1;
if(map[i][j]==map[i][j+1]){
rights[i][j]=j;
r=j+1;
}
else{
rights[i][j]=r-1;
}
}
}
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
}
}
return ret;
} int main(){
int ansR,ansB,ansC,kase=0;
int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
getchar();
for(int i=1;i<=n;i++)
gets(map[i]+1);
ansR=CountSingle(n,m,'R');
ansB=CountSingle(n,m,'B');
ansC=CountCross(n,m);
/* for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++)
cout<<heights[i][j]<<' ';
cout<<endl;
}
cout<<endl;
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++)
cout<<lefts[i][j]<<' ';
cout<<endl;
}
cout<<endl;
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++)
cout<<rights[i][j]<<' ';
cout<<endl;
}
cout<<ansC<<endl;*/
cout<<"Case #"<<++kase<<": "<<max(ansR,max(ansB,ansC))<<endl;
}
return 0;
}

  

最新文章

  1. [转]&#39;&#39;\\=DevExpress 中 汉化包 汉化方法
  2. 一个textview实现文字在图片上面的效果
  3. 【linux】常见的网络管理命令
  4. Flink Program Guide (10) -- Savepoints (DataStream API编程指导 -- For Java)
  5. WPF中的布局控件(转)
  6. CCIE路由实验(7) -- MPLS VPN
  7. windows上定时执行php文件
  8. Mybatis源码解析-MapperRegistry注册mapper接口
  9. Python文件复制(txt文件)
  10. [转]Oracle 重建索引的必要性
  11. int指令
  12. 听《津津乐道》ThinkPad专题节目有感
  13. Excel表格中依据某一列的值,将这列中一样的数据放在一个文件中。
  14. C语言错题集
  15. Java设计模式学习记录-状态模式
  16. ROIAlign, ROIPooling及ROIWarp对比
  17. C#程序优化的50种方案
  18. How to modify analog output range of Arduino Due
  19. wannafly 17D 01序列2
  20. 使用 VS2012 开发 IDA GUI 插件 WIN32 SDK 和 内置函数 AskUsingForm_c

热门文章

  1. JAVA实现将GeoHash转化为相应的经纬度坐标
  2. 3D数学读书笔记——矩阵进阶
  3. SDUT OJ 1124 飞越原野 (三维BFS练习)
  4. linux中设置TAB键的宽度
  5. Android学习之——优化篇(1)
  6. java 用JNA调用dll 参考文档
  7. MySql悲观锁总结与实践
  8. 基于Apache CXF的Web Service服务端/客户端
  9. php如何判断两个时间戳是一天
  10. spring-boot系列:(一)整合dubbo