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
First of all, it must be converted to the maximum 0/1 sub-matrix, but how to convert it??? This is a great practice. After the matrix is dyed in black and white, the test instructions checkerboard needs black and white, that is, the adjacent black and white lattice colors are different. Assuming we reverse the value of the black (white) lattice, the legal state is the same as the black and white lattice color, that is, to find a maximum of 0/1 sub-matrices ... So the maximum 0/1 sub-matrix How to find out in O (n*m) time, we can DP. UP[I][J] means that from (I,J) the highest can be stretched a few squares, le[i][j],ri[i][j] represents up[i][j] This suspension can be left and right to move to where, the largest rectangle of the polygon base Ans1=max (up[i][j]* (ri[i][j]-le[i ][j]+1), ans1), Maximum square area ans2=max (min (Up[i][j], (ri[i][j]-le[i)) 2,ans2). Do it again for 0 and 1. Transfer very well write:
1 for(inti =1; I <= n;++i)2 {3 intLo =0, RO = m+1;4 for(intj =1; J <= m;++j)5 {6 if(S[i][j] = = sign) up[i][j] = le[i][j] =0, lo =J;7 ElseUP[I][J] = i==1?1: up[i-1][j]+1, le[i][j] = i==1? lo+1: Max (le[i-1][j],lo+1);8 }9 for(intj = m;j;--j)Ten { One if(S[i][j] = = sign) ri[i][j] = m+1, RO =J; A ElseRI[I][J] = i==1? ro-1: Min (ri[i-1][j],ro-1); - intA = Up[i][j],b = ri[i][j]-le[i][j]+1, p =min (A, b); -ANS1 = max (ans1,p*p); ANS2 = Max (ans2,a*b); the } -}
View Code
Total Code:
1#include <iostream>2#include <cstdio>3#include <cstdlib>4 using namespacestd;5 6 #defineMAXN 20107 intS[MAXN][MAXN],LE[MAXN][MAXN],RI[MAXN][MAXN];8 intUp[maxn][maxn],n,m,ans1,ans2;9 TenInlinevoidWork () One { A for(inti =1; I <= n;++i) - { - for(intj =1; J <= m;++j) the { - if(i = =1|| S[I][J] = = s[i-1][J]) Up[i][j] =1; - ElseUP[I][J] = up[i-1][j]+1; - } + } - } + AInlinevoiddeal () at { - for(inti =1; I <= n;++i) - for(intj =1; J <= m;++j) - if((i + j) &1) S[i][j] ^=1; - } - inInlinevoidWorkintSign ) - { to for(inti =1; I <= n;++i) + { - intLo =0, RO = m+1; the for(intj =1; J <= m;++j) * { $ if(S[i][j] = = sign) up[i][j] = le[i][j] =0, lo =J;Panax Notoginseng ElseUP[I][J] = i==1?1: up[i-1][j]+1, le[i][j] = i==1? lo+1: Max (le[i-1][j],lo+1); - } the for(intj = m;j;--j) + { A if(S[i][j] = = sign) ri[i][j] = m+1, RO =J; the ElseRI[I][J] = i==1? ro-1: Min (ri[i-1][j],ro-1); + intA = Up[i][j],b = ri[i][j]-le[i][j]+1, p =min (A, b); -ANS1 = max (ans1,p*p); ANS2 = Max (ans2,a*b); $ } $ } - } - the intMain () - {WuyiFreopen ("1057.in","R", stdin); theFreopen ("1057.out","W", stdout); -scanf"%d%d",&n,&m); Wu for(inti =1; I <= n;++i) - for(intj =1; J <= M;++j) scanf ("%d", s[i]+j); AboutDeal (); Work0); Work1); $printf"%d\n%d", ans1,ans2); - fclose (stdin); fclose (stdout); - return 0; -}
View Code
Bzoj 1057 Board Making