Description
Chess is one of the oldest game games in the world, with Chinese Weiqi, chess and Japanese chess. It is said that chess originated from the thought of the I Ching, the chessboard is a 8*8 size of black and white square, corresponding to 8,864 gua, black and white corresponding yin and yang. And our protagonist, the little Q, is a chess enthusiast. As a top-notch player, he was not content with the usual chessboard and rules, so he and his good friend, Little W, decided to expand the chessboard to fit their new rules. Little Q found a rectangular piece of paper made up of squares of n*m, each of which was painted with one of the two colors of black and white. Little Q wanted to cut some of this paper as a new chessboard, and of course he wanted the chessboard to be as big as possible. But little Q has not decided to find a square chessboard or a rectangular chessboard (of course, the chessboard must be black and white, that is, the adjacent lattice is different), so he hopes to find the largest square checkerboard area and the largest rectangular checkerboard area, which will determine which is better. So little Q found the upcoming National Information Science Competition you, can you help him?
Input
The first line contains two integers n and m, each representing the length and width of the rectangular paper. The next n rows contain a 01 matrix of N * m, representing the color of the rectangle paper (0 for White and 1 for black).
Output
Contains two lines, each containing an integer. The first behavior can be found in the area of the largest square checkerboard, and the second behavior can be found in the area of the largest rectangular checkerboard (note that squares and rectangles can intersect or contain).
Sample Input3 3
1 0 1
0 1 0
1 0 0Sample Output4
6HINT
For 100% of data, N, m≤2000
Ideas
This time Zhejiang's topic actually did not test data structure, simply BU Science.
however is a classic topic of deformation, the maximum of all 0/1 sub-matrices.
We notice that if you flip all of the grid colors (number of rows + columns)%2==1, a chessboard becomes the same color. Then it becomes the maximum 0/1 sub-matrix.
But this I can not write, anger Baidu.
O (n2) algorithm. Like the Perfection 1 matrix.
Use h[i] to indicate the number of rows in the current row I column up to 1, L[i] indicates which row to the left (that is, H[l[i]]~h[i] <=h[i]), R[i] indicates to which row to the right.
Processing one row at a time, l[i from left to right], processing from right to left R[i].
When H[i]<=h[l[i]-1], l[i]=l[l[i]-1], to the right.
See code for details.
1#include <iostream>2#include <cstring>3#include <string>4#include <cstdio>5#include <cstdlib>6#include <cmath>7#include <algorithm>8#include <queue>9#include <stack>Ten#include <map> One#include <Set> A#include <list> -#include <vector> -#include <ctime> the#include <functional> - #definePRITNF printf - #defineScafn scanf - #defineSACNF scanf + #definefor (i,j,k) for (int i= (j); i<= (k);(i) + +) - #defineClear (a) memset (A,0,sizeof (a)) + using namespacestd; Atypedef unsignedintUint; at Const intinf=0x3fffffff; - ///==============struct declaration============== - - ///==============var declaration================= - Const intmaxn=2050; - intRow,col; in intPIC[MAXN][MAXN]; - intH[MAXN],L[MAXN],R[MAXN]; to ///==============function declaration============ + intFindsquare (intval); - intFindrectangle (intval); the ///==============main code======================= * intMain () $ {Panax Notoginseng #definefile__ - #ifdef file__ theFreopen ("input","R", stdin); +Freopen ("Output","W", stdout); A #endif thescanf"%d%d",&row,&col); + for(intI=1; i<=row;i++) - for(intj=1; j<=col;j++){ $scanf"%d",&pic[i][j]); $ if((I+J) &1) pic[i][j]=!Pic[i][j]; - } -printf"%d\n", Max (Findsquare (1), Findsquare (0))); theprintf"%d\n", Max (Findrectangle (1), Findrectangle (0))); - return 0;Wuyi } the ///================fuction code==================== - intFindsquare (intval) { Wumemset (H,0,sizeof(h));intans=0; - for(intI=1; i<=row;i++){ About for(intj=1; j<=col;j++) $ if(Pic[i][j]==val) h[j]=h[j]+1; - Elseh[j]=0; - for(intj=1; j<=col;j++){ -l[j]=J; A while(l[j]>1&&h[l[j]-1]>=H[j]) +l[j]=l[l[j]-1]; the } - for(intj=col;j>=1; j--){ $r[j]=J; the while(r[j]<col&&h[r[j]+1]>=H[j]) ther[j]=r[r[j]+1]; the } the for(intj=1; j<=col;j++){ - intw=r[j]-l[j]+1; inw=min (w,h[j]); theAns=max (ans,w*W); the } About } the returnans; the } the intFindrectangle (intval) { +memset (H,0,sizeof(h));intans=0; - for(intI=1; i<=row;i++){ the for(intj=1; j<=col;j++)Bayi if(Pic[i][j]==val) h[j]=h[j]+1; the Elseh[j]=0; the for(intj=1; j<=col;j++){ -l[j]=J; - while(l[j]>1&&h[l[j]-1]>=H[j]) thel[j]=l[l[j]-1]; the } the for(intj=col;j>=1; j--){ ther[j]=J; - while(r[j]<col&&h[r[j]+1]>=H[j]) ther[j]=r[r[j]+1]; the } the for(intI=1; i<=col;i++){94 intw=r[i]-l[i]+1; theAns=max (ans,w*h[i]); the } the }98 returnans; About}
BZOJ1057
"ZJOI2007" Checkerboard making BZOJ1057