Input
Output
Sample Input
2
5 6
R F F F F F
F F F F F F
R R R F F F
F F F F F F
F F F F F F
5 5
R R R R R
R R R R R
R R R R R
R R R R R
R R R R R
Sample Output
45
0
题意: 找出矩形形状的区域, 找出最大一个区域, 计算面积*3.
解题思路:
1. 这类问题其实目的就一个, 算过的地方不想再重复计算, 降低时间复杂度. 那么就从第一行
第一列开始. 一个一个往下计算, 同时我们记录每个点(i,j); 可以往上, 往左偏移和往右
偏移到的位置(矩形扩张).
2. 设up[i][j]: 表示当前(i,j)往上延伸最大长度; 如果(i,j区域空置)up[i][j] = up[i-1][j]+1;
设leftSide[i][j]: 表示往左扩张; (i,j) = F; leftSide[i][j]=max(leftSide[i-1][j], lside+1);
设rightSide[i][j]: 表示往右扩张; (i, j) = F; rightSide[i][j]=min(rightSide[i-1][j], rside-1)
代码:
#include
#include
#include
using namespace std;
#define MAX 1005
int n, m;
int g[MAX][MAX];
int up[MAX][MAX], leftSide[MAX][MAX], rightSide[MAX][MAX];
inline int min(int a, int b)
{
return a < b ? a : b;
}
inline int max(int a, int b)
{
return a > b ? a : b;
}
int main()
{
// freopen("input.txt", "r", stdin);
int caseNum;
scanf("%d", &caseNum);
while(caseNum--)
{
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
{
char ch[3];
scanf("%s", ch);
if(ch[0] == 'F') g[i][j] = 0;
else g[i][j] = 1;
}
}
int result = 0;
for(int i = 0; i < n; ++i)
{
int lside = -1, rside = m;
for(int j = 0; j < m; ++j)
{
if(g[i][j] == 1)
{
leftSide[i][j] = 0;
up[i][j] = 0;
lside = j;
}
else
{
up[i][j] = (i == 0 ? 1 : (up[i-1][j]+1));
leftSide[i][j] = (i == 0 ? lside+1 : max(leftSide[i-1][j], lside+1));
}
}
for(int j = m-1; j >= 0; --j)
{
if(g[i][j] == 1)
{
rightSide[i][j] = m;
rside = j;
}
else
{
rightSide[i][j] = (i == 0 ? rside-1 : min(rightSide[i-1][j], rside-1));
result = max(result, up[i][j]*(rightSide[i][j]-leftSide[i][j]+1));
}
}
}
printf("%d\n", result*3);
}
return 0;
}