G.MaxArea
1000ms / 65536KB / C, C++ or JAVA
Editor: dovegx
Description
Cellsof a matrix are called adjacent if they share an edge. A matrix of zeroes andones is called a chess matrix if there are no two adjacent cells that share thesame value. Hence, in a chess matrix the zeroes and ones have to alternate inthe same way the colors alternate on a chessboard.
You are given a matrix board with n rows and m columns that represents arectangular grid of cells, with a 0 or a 1 in each cell. Each character of eachelement of board will be either '0' or '1'. In this grid we selected somerectangular subgrid that is a chess matrix. Calculate the largest possible areaof the selected subgrid.
Input Description
Input contains multiple test cases.
For each case the first line contains two integer n, m. ( 1 <= n, m <=300 )
The next n lines each contains m integers, either ‘0’ or ‘1’.
Output Description
For each case output one integer means thelargest possible area of the selected subgrid that is a chess matrix.
Sample Input
3 3
0 1 1
1 0 1
0 0 0
2 4
0 1 0 1
1 0 1 0
Sample Output
4
8
题解:先处理每个点向右的合法长度,然后枚举左上角,向下撸就行了
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <queue>
#include <numeric>
#include <list>
#include <functional>
using namespace std;
#define eps 1e-8
#define LL long long
#define ULL unsigned long long
#define mod 1000000007
#define MP make_pair
#define PII pair<int, int>
#define INF 0x3f3f3f3f
int a[305][305];
int col[305][305], row[305][305];
int main()
{
int n, m;
while (~scanf("%d%d", &n, &m))
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
scanf("%d", &a[i][j]);
}
}
for (int k = 0; k < n; ++k) // 枚举合法长度
{
for (int i = 0; i < m; ++i)
{
int tmp = 1;
for (int j = i + 1; j < m; ++j)
{
if (a[k][j] != a[k][j-1])
{
tmp++;
}
else break; //这个break一开始不小心删掉了,害我找了一晚上
}
row[k][i] = tmp; //另开数组保存
}
}
int ans = 0;
for (int k = 0; k < m; ++k)
{
for (int i = 0; i < n; ++i)
{
//int tmp = 1;
int minn = row[i][k];
ans = max(minn, ans); //要注意先比较,因为有可能 n = 1,然后下面for就不会继续。
for (int j = i + 1; j < n; ++j)
{
if (a[j][k] != a[j-1][k])
{
minn = min(minn, row[j][k]);
ans = max(ans, minn*(j-i+1));
}
else break;
}
}
}
printf("%d\n", ans);
}
return 0;
}