Title Description
Chess is one of the world's oldest game of chance, and Chinese chess, chess and the chess the equally famous in Japan. It is said that chess originated in the idea of the I Ching, the board is a 8 \ times 88 × 8-sized black and white squares, the corresponding 88 64 hexagrams, yin and yang, black and white correspond.
And our hero 小Q
, what aficionados of chess. As a leading expert, he was not satisfied with the general rules of the board, so he and his friends 小W
decided to expand the board to accommodate their new rules.
小Q
Find one of N \ M Times N × M rectangular sheet of lattice squares, each of which is coated with a grid of black and white one of two colors. 小Q
As part of the new board wanted to cut in this paper, of course, he hopes this board as large as possible.
But 小Q
it has not yet decided the board was looking for a square or a rectangle board (of course, no matter what, the board must be black and white, that is adjacent lattice different color), so he hopes to find the largest area and the largest square chessboard rectangular checkerboard area, to determine which is better.
So 小Q
find you are about to participate in the national informatics contest, can you help him?
Resolve
Routine questions.
Hanging line method
Application: Solving a matrix largest sub-matrix qualifying.
Continued to narrow the width of the popular talk feasible matrix, the matrix approach is feasible increase in height. A bit like a monotonous stack, while maintaining the maximum width of the sub-matrix to increase the legal maximum height.
Specifically we generally want to maintain an arbitrary point \ ((i, j) \ ) of three properties: given the nature of the subject at prerequisite to ensure that, to the left of the dot can be extended furthest point \ (l [i] [J] \) , the right to extend the furthest point \ (R & lt [I] [J] \) , extended up to the farthest point \ (up [I] [J] \) .
State transition equation:
\ [L [I] [J] = \ max (L [I] [J], L [. 1-I] [J]) R & lt \\ [I] [J] = \ min (R & lt [ i] [j], r [
i-1] [j]) \\ up [i] [j] = up [i-1] [j] +1 \] prior to this, we have to pre-processing the single-line ( It will be appreciated do \ (up [I] [J] =. 1 \) ) for each point \ (l [i] [j ] \) and \ (R & lt [I] [J] \) .
Reference Code
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define N 2010
using namespace std;
int mp[N][N],l[N][N],r[N][N],up[N][N],n,m;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j) scanf("%d",&mp[i][j]),l[i][j]=r[i][j]=j,up[i][j]=1;
for(int i=1;i<=n;++i)
for(int j=2;j<=m;++j)
if(mp[i][j]!=mp[i][j-1])
l[i][j]=l[i][j-1];
for(int i=1;i<=n;++i)
for(int j=m-1;j>0;--j)
if(mp[i][j]!=mp[i][j+1])
r[i][j]=r[i][j+1];
int ans1=0,ans2=0;
for(int i=2;i<=n;++i)
for(int j=1;j<=m;++j){
if(mp[i][j]!=mp[i-1][j]){
l[i][j]=max(l[i][j],l[i-1][j]);
r[i][j]=min(r[i][j],r[i-1][j]);
up[i][j]=up[i-1][j]+1;
}
int w=r[i][j]-l[i][j]+1;//宽
int h=up[i][j];//高
int t=min(w,h);
ans1=max(ans1,t*t);
ans2=max(ans2,w*h);
}
cout<<ans1<<endl;cout<<ans2<<endl;
return 0;
}