Maximum sub-matrix, maximum continuous sub-array, dynamic programming beginner, poj1050

Source: Internet
Author: User

Title Description: A n*n matrix is given, which is required to find the largest and most sub-matrices.

For example:

Such a matrix, the maximum sub-matrix of the and is 15;

This problem can be reminiscent of the maximum contiguous subarray, and the maximum subarray is http://www.cnblogs.com/tz346125264/p/7560708.html in the previous article.

Analysis: The maximal sub-matrix can be seen as the largest continuous sub-array to expand to a two-dimensional array, because the nature of the matrix also in the horizontal vertical upward need continuous, then we can find a way to simplify the two-dimensional array to seek continuous sub-arrays.

Thinking:

1. Requires the maximum sub-matrix, must ensure that each matrix is browsed to, in order to ensure that the runtime as far as possible do not repeat browsing the same matrix, it is necessary to make rules, the rules are set to use I for the starting line, J for terminating the row, j>=i, and then using K to traverse the column, you can overwrite all matrices.

2. To make a continuous sub-array operation, for example, set i=2,j=4 then this matrix is so, then into a continuous sub-array is 4 (9-4-1), 11 (2+1+8), 10 ( -6-4+0), 1 (2+1-2)

The maximal contiguous subarray of this continuous array is the maximum sub-matrix of the matrix (with i=2 as the starting line and j=4 as the terminating line), so that the largest sub-matrix of the entire matrix is the largest of these maximal sub-matrices.

On the code:

#include <iostream>#include<cstring>using namespacestd;intMain () {intN; inta[101][101]; inttemp[101]; CIN>>N;  for(intI=1; i<=n;i++){         for(intj=1; j<=n;j++) {cin>>A[i][j]; /*Why do we add them? In order to find a continuous maximum subarray, the number of rows I to J needs to be summed sum (I,J), and this sum{i,j} can have sum{0,j}-sum{0,i} to be added in advance to make it easier to simplify*/A[i][j]+=a[i-1][j]; }    }    intmax =-10000;  for(intI=1; i<=n;i++){         for(intj=i+1; j<=n;j++) {memset (temp,0,sizeof(temp)); //To find the maximum contiguous subarray operation             for(intk=1; k<=n;k++){                if(temp[k-1]>=0) {Temp[k]=temp[k-1]+a[j][k]-A[i][k]; }Else{Temp[k]=a[j][k]-A[i][k]; }                if(temp[k]>max) {Max=Temp[k]; } }}} cout<<max<<Endl; return 0;} 

Maximum sub-matrix, maximum continuous sub-array, dynamic programming beginner, poj1050

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.