Total 1 sub-matrices with the largest area-nine degrees OJ 1497

Source: Internet
Author: User

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:

The input may contain multiple test samples.
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, and the adjacent two numbers are strictly separated by a space.

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: 0 4

Problem-Solving ideas: reproduced from http://www.cnblogs.com/fstang/archive/2013/05/19/3087746.html

The method is:

1, first read the 0/1 matrix X, for each non-0 element x[i][j], update it to: In the bank, it precedes the number of successive 1 of +1 (+1 means to count itself)

For example, if a behavior 0 1 1 0 1 1 1, then update to 0 1 2 0 1 2 3

2. For each non-0 element x[i][j], scan up and down in column J until the number smaller than itself is encountered, and if Y line is scanned, then a full 1 sub-matrix (+1 for the row) of size x[i][j]* (y+1) is obtained.

For example, if a column is [0 3 4 3 5 2 1] ' (for convenience, the columns are represented as a column vector), we process the 4th element of this column, which is 3, which can scan up to 2 elements, scan down 1 elements, and get a 4x3 full 1 sub-matrix.

3. Take one of the largest values in these numbers.

Thought probably as shown (0 in the blanks is not marked)

In contrast to the example given in step 2, the blue arrows indicate a downward scan, and a black box represents the final 1 sub-matrices.

Why is it right to do this?

Think about the biggest 1 sub-matrix of all, can you find it in this way? --sure it can.

A maximum of 1 sub-matrices, which must be one of four boundaries, can no longer be extended, as

Assuming that the full 1 sub-matrix is the maximum sub-matrix, then the left edge of the column must have one or more 0 (otherwise you can extend a column to the left to get a larger full 1 matrix)

There is a similar situation for the other 3 borders.

Then look at the picture with a black circle marked 1 (which is characterized by: and the left side of the left edge of a 0 in the same row), starting from this 1, according to the previous method, up and down scan, you can get this sub-matrix. So, surely you can find it.

Here is my code, the actual implementation of the time, in order to improve efficiency, estimated the upperbound, this upperbound is: In the current column,

A continuous non-0 sequence containing x[i][col], such as a column [0 3 4 3 5 2 1] ', followed by 6 upperbound
3 + 4 + 3 + 5 + 2 + 1 = 18, for 0 elements, no upperbound required

1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6     intn,m;7      while(cin>>n>>m) {8         int**array=New int*[n];9         int**upperbound=New int*[n];Ten          for(intI=0; i<n;i++){ Onearray[i]=New int[m]; Aupperbound[i]=New int[m]; -              for(intj=0; j<m;j++){ -Cin>>Array[i][j]; theupperbound[i][j]=0; -             } -         } -         //Prepare: +          for(intI=0; i<n;i++){ -              for(intj=1; j<m;j++){ +                 if(array[i][j]==1&&array[i][j-1]!=0) array[i][j]=array[i][j-1]+1; A             } at         } -         //Calculate Upperbound -          for(intj=0; j<m;j++){ -              for(intI=0; i<n;i++){ -                 if(array[i][j]==0)Continue; -                 Else{ in                     intsum=0, temp=i; -                      while(temp<n&&array[temp][j]>0){ tosum+=Array[temp][j]; +temp++; -                     } the                      for(intk=i;k<temp;k++){ *upperbound[k][j]=sum; $                     }Panax NotoginsengI=temp; -                 } the             }     +         } A  the         intMaxarea=0; +          for(intI=0; i<n;i++){ -              for(intj=0; j<m;j++){ $                 if(array[i][j]!=0&&maxarea<Upperbound[i][j]) { $                     intCnt=1, val=Array[i][j]; -                      for(introw=i-1;row>0; row--){ -                         if(Array[row][j]>=val) cnt++; the                         Else -                              Break;//There must be a break here.Wuyi                     } the                      for(introw=i+1; row<n;row++){ -                         if(Array[row][j]>=val) cnt++; Wu                         Else -                              Break;//There must be a break here. About                     } $                     if(Cnt*val>maxarea) maxarea=cnt*Val; -                 } -             } -         } Acout<<Maxarea; +     } the     return 0; -      $}
View Code

Total 1 sub-matrices with the largest area-nine degrees OJ 1497

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.