The maximum sub-matrix problem (maximum continuous sequences and linear dp)

Description] [Title
of known size of the matrix is defined as all elements and matrix. Given a matrix, your task is to find the largest non-empty (the size of at least 1 × 1) sub-matrix.

For example, the following matrix of 4 × 4

0 0 -2 -7
. 9 -6 2 2
-4. 1. 1 -4
-1 0 -2. 8
largest sub-matrix is

2. 9
-4. 1
-1. 8
the sub-matrix size is 15.

[Input]
input is an N × N matrix. The first row gives the input N (0 <N≤100). Several rows back again sequentially (from left to right is given first integer number N of the first row, from left to right and then given a second row of N integers ......) given integers N2 matrix, separated by whitespace between integer (empty spaces or lines). Integer matrix is known in the range [-127,127].

[Output]
the size of the largest sub-output matrix.

[Sample] input
. 4
0 -2 0 -7
. 9 -6 2 2
-4. 1. 1 -4
-1 0 -2. 8
[output] Sample
15
Title Analysis:
the problem with the greatest need for a continuous sequence of one-dimensional and issues as a basis for a good understanding of the state transition equation. With DP [i] indicates the maximum contiguous subsequence and 1 to i. dp [i] = max (dp
[i-1] + a [i], a [i]) I explain simply, the array values are determined for each of a [i] is, the relative value of dp uncertain, so the maximum consecutive sequence to i 1 and DP is equal to 1 i-1 [i-1] and the value a [i] and, but dp [i-1] <0 can be with a [i] as a new element at the beginning of the sequence. To conclude is this:
DP [I] = DP [I-. 1] + A [I], (DP [I-. 1]> 0)
DP [I] = A [I], (DP [I-. 1] < 0) that takes the maximum value in the two formulas.
The biggest problem is the need for sub-matrix two-dimensional problem into a one-dimensional compression, all the possibilities and all the stores listed in the matrix with a two-dimensional array. Here set F [i] [j] denotes the front row i and column j of cumulative

for(int i=1;i<=n;i++)//控制子矩阵上界
    {
        memset(f,0,sizeof(f));//每更新一次上界必须给f数组清零
        for(int j=i;j<=n;j++)
        {
            for(int k=1;k<=n;k++)//每一列
            f[j][k]=f[j-1][k]+a[j][k];
            sum(j);//求从i到j行的最大连续子段和(即最大子矩阵)
        }
    }

Wherein the sum (int) function is a function of seeking maximum continuous sequence, without the tube.
So that you can enumerate all the circumstances of the matrix.
Complete code:

#include<iostream>
#include<cstring>
using namespace std;
int maxl=-99999;
int n;
int a[111][111],f[111][111],dp[111];
//a用来存储矩阵。f[i][j]表示前i行j列的累加和,dp[i]表示前i行最大子矩阵的和。
void sum(int j)
{
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)
    {
        dp[i]=max(dp[i-1]+f[j][i],f[j][i]);
        maxl=max(dp[i],maxl);
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        cin>>a[i][j];
    for(int i=1;i<=n;i++)//控制子矩阵上界
    {
        memset(f,0,sizeof(f));//每更新一次上界必须给f数组清零
        for(int j=i;j<=n;j++)
        {
            for(int k=1;k<=n;k++)//每一列
            f[j][k]=f[j-1][k]+a[j][k];
            sum(j);//求从i到j行的最大连续子段和(即最大子矩阵)
        }
    }
    cout<<maxl;
    return 0;
}
Published 42 original articles · won praise 42 · views 9319

Guess you like

Origin blog.csdn.net/amazingee/article/details/104311743