题目描述:
Given a N×MN×M binary matrix. Please output the size of second large rectangle containing all "1""1".
Containing all "1""1" means that the entries of the rectangle are all "1""1".
A rectangle can be defined as four integers
x1,y1,x2,y2x1,y1,x2,y2 where 1≤x1≤x2≤N1≤x1≤x2≤N and 1≤y1≤y2≤M1≤y1≤y2≤M. Then, the rectangle is composed of all the cell (x, y) where x1≤x≤x2x1≤x≤x2 and y1≤y≤y2y1≤y≤y2. If all of the cell in the rectangle is "1""1", this is a valid rectangle.
Please find out the size of the second largest rectangle, two rectangles are different if exists a cell belonged to one of them but not belonged to the other.
输入描述:
The first line of input contains two space-separated integers N and M.
Following N lines each contains M characters cijcij.
1≤N,M≤10001≤N,M≤1000
N×M≥2N×M≥2
cij∈"01"cij∈"01"
输出描述:
Output one line containing an integer representing the answer. If there are less than 2 rectangles containning all "1""1", output "0""0".
样例:
题目大意:给一个n*m的01矩阵,求所有全为1组成的矩阵中第二大的1矩阵的面积,如果1矩阵不足2个,输出0。
解题代码:
#include<iostream> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const int _ = 1005; int dp[_][_]; int secArea = -1,maxArea = -1; int main() { int n,m; memset(dp,0,sizeof(dp)); cin>>n>>m; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { scanf("%1d",&dp[i][j]); dp[i][j]+=dp[i][j]*dp[i-1][j]; } }//预处理dp[i][j]为该点上方路连续1的最大值 for(int i = 1; i<=n; i++) { for(int j = 1; j<=m; j++) { int height = dp[i][j]; int wide = 1; //当前矩阵的高和宽 while(height) { int Area = height*wide; if(Area>maxArea) { secArea = maxArea; maxArea = Area; } else if(Area>secArea) { secArea = Area; } height = min(height,dp[i][j-wide]); wide++; } } } cout<<(secArea == -1 ? 0 : secArea)<<endl; return 0; }