-
Title Description:
-
In a matrix of M * N, all elements are only 0 and 1, from this matrix to find an area of the largest total 1 sub-matrix, the so-called maximum refers to the number of elements 1 the most.
-
input:
-
For each test case, the first line entered is two integers m, n (1<=m, n<=1000): Represents the size of the matrix that will be entered. The
Matrix has m rows, each row has n integers, 0 or 1, respectively, separated by a single space between the two adjacent numbers.
-
Output:
-
Corresponds to each test case, the number of elements in the output matrix of the largest total 1 sub-matrices.
-
Sample input:
-
2 20 00 04 40 0 0 00 1 1 00 1 1 00 0 0 0
-
-
Sample output:
-
-
04
-
-
-
-
-
-
Count the 1 consecutive numbers in each row
-
-
For example
-
-
1 1 1 1 1 0 1
-
-
-
-
0 0 1 1 1 0 1
-
-
1 1 0 0 1 0 0
-
-
Get
-
-
1 2 3 4 5 0 1
-
-
0 0 1 2 0 1 1
-
-
0 0 1 2 3 0 1
-
-
1 2 0 0 1 0 0
-
-
And then follow the column accumulation to get continuous
-
-
1 2 5 8 5 0 3
-
-
0 0 5 8 0 1 3
-
-
0 0 5 8 4 0 3
-
-
1 2 0 0 4 0 0
-
-
This is done by comparing it with the MAXN to see if it counts.
-
-
Then if there is a need to calculate, then look at this position down and go online, find in the case of the second rectangle this column is small to this number when the exit
-
-
Because this number is the number of 1 of the horizontal over the statistic
-
-
The 1 matrix can be built at the present length only when it is greater than or equal to itself.
-
-
So get the code as follows
-
-
-
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int map[1005][1005]; int col[1005][1005];int n,m,maxn;void set_col (int p) {int i,j,sum; for (i = 1; i<=n; i++) {if (!map[i][p]) continue; j = i; sum = 0; while (Map[j][p] && j<=n) {sum+=map[j][p]; j + +; } for (int k = i; k<j; k++) col[k][p] = sum; i = j; }}int Find (int r,int c) {int i,j,val=map[r][c]; int cnt = 1; for (i = r-1; i>0; i--) {if (val>map[i][c]) break; cnt++; } for (i = r+1; i<=n; i++) {if (val>map[i][c]) break; cnt++; } return val*cnt;} int solve () {int i,j; MAXN = 0; for (i = 1; i<=n; i++) {for (j = 1; j<=m; j + +) {if (Maxn<col[i][j] && map[i][j ]) {int val = find (i,j); MAXN = max(Maxn,val); }}} return MAXN;} int main () {int i,j; while (~SCANF ("%d%d", &n,&m)) {for (i = 1; i<=n; i++) {for (j = 1; j<=m; j + +) scanf ("%d", &map[i][j]); } for (i = 1; i<=n; i++) {for (j = 2; j<=m; j + +) {if (map[i][j]==1 && map[i][j-1]) map[i][j]=map[i][j-1]+1; }} for (i = 1; i<=m; i++) Set_col (i); printf ("%d\n", Solve ()); } return 0;}
Nine degree 1497: the largest full 1 sub-matrix of the area