本题和1506思路完全一样。只不过需要把图中的点转化为高度,以每行底来查找最大面积*3;
都是利用记忆化查找的思想来查找最左或者最右的边界,具体解释看代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <cmath>
using namespace std;
#define cir(a,b) memset(a,b,sizeof a)
const int maxn = 1000+10;
int T;
int m,n;
int s[maxn][maxn];
int h[maxn][maxn];
int l[maxn];
int r[maxn];//l and r 数组是保存每列的最左和最右边的边界的下标
int dp[maxn];
char ch;
int main()
{
cin >> T;
while(T--)
{
cin >> m >> n;
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
cin >> ch;
if(ch == 'R') s[i][j] = 0;
else s[i][j] = 1;
}
}
int number = 0;
for(int j=1; j<=n; j++)
{
number = 0;
for(int i=1; i<=m; i++)
{
if(s[i][j]==1)
{
++number;
h[i][j] = number;
}
else
{
number = 0;
h[i][j] = number;
}
}
}
// for(int i=1;i<=m;i++)
// {
// for(int j=1;j<=n;j++)
// {
// cout << h[i][j] <<" ";
// }
// cout << endl;
// }
int Max = -1;
for(int i=1; i<=m; i++)
{
cir(l,0),cir(r,0);//每次清空高度
l[1] = 1;//最左边的边界一定是自己
for(int j=2; j<=n; j++)
{
int t1=j;
while(t1>1&&h[i][t1-1]>=h[i][j])
{
t1 = l[t1-1];// 如果某列的左边的列的高度大于等于本列,所以本列左边的边界一定也是大于等于本列的
//cout << t1 <<endl;
}
l[j] = t1;//记录最左边界
}
r[n] = n;最右边的边界一定是自己
for(int j=n-1; j>=1; j--)
{
int t2=j;
while(t2<n-1&&h[i][t2+1]>=h[i][j])
{
t2 = r[t2+1];// 如果某列的右边的列的高度大于等于本列,所以本列右边的边界一定也是大于等于本列的
// cout << "t2" << " " << t2 <<endl;
}
r[j] = t2;//记录最右边界
}
for(int j=1; j<=n; j++)
{
if( (r[j]-l[j]+1) * h[i][j] > Max )
{
//cout << r[j] << " " << l[j] << " "<< h[i][j] << endl;
Max = (r[j]-l[j]+1) * h[i][j] ;
}
}
}
cout << Max*3 << endl;
}
return 0;
}