The beauty of programming-the maximum value of the sum of the words group (two-dimensional, dynamic programming)

Source: Internet
Author: User

Extend the previous question, looking for the sum of the data in the two-dimensional array, from (x1, y1) to (x2, y2) to determine the maximum value of the rectangle area.

The solution gives a trick of creating an array as large as the original array to hold (0, 0) to the sum of (x, y), i.e. p[x][y] = sums (x[0][0]–> x[x][y]). Then use sum = Ps[i_max][j_max]-ps[i_min-1][j_max]–ps[i_max][j_min-1] + ps[i_min-1][i_min-1]. Do not know why the book to let the array starting from 1, and then set the bounds of the array 0.

Solution two uses the idea of divided treatment, or dynamic planning it. A boundary is assumed, the boundary is traversed by a poor lift, and the selected data is transformed into one-dimensional data, which is analyzed according to the algorithm in the previous section.

Extension issues:

1. Once the closure is connected, the boundary can only be determined up and down, and then solved by one-dimensional solution.

2. Up and down also connected, it is necessary to two directions in accordance with the situation of the first and the next consideration.

3, 4. Three-dimensional and four-dimensional situation, at least can be traversed in two directions, on the other side up first sum and then the one-dimensional situation to solve it.

1. Brief description

Given a matrix of n*n (0<n<=100), find a sub-matrix of this matrix, and the maximum value of each element of this sub-matrix, and the maximum output.
Example:
0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2
The maximum sub-matrices are:
7 |
-4 1
-1 8

2. Principle

The maximum sub-matrix and the two-dimensional extension are the largest subsequence and.
If all the sub-matrices are exhaustive, there are C (n,2) *c (n*2)/2 sub-matrices, which is the n^4 sub-matrix, which does not count the sum time of each sub-matrix, to O (n^4).
The idea of converting to maximal sub-sequence solution is to fix the range of column I to column J, look for the maximal sub-matrix in this range, the search process, sum the elements in column I of each row to the elements of column J, and transform it into one-dimensional case. Because of the combination of C (n,2) columns, and the time required for the one-dimensional subsequence to be n, the overall time complexity is O (n^3).

3. Recursive formulas

The subscript of a is starting from 0, in order to increase the Sentinel, the subscript of the f,p,q starts from 1.
· A[n][n] represents the input matrix.
· F[N+1][N+1],F[I][J] represents the element and from column No. 0 of row i to column J.
F[i][j]=0,j=0
F[i][j]=a[i-1][j-1], J=1
F[i][j]=f[i][j-1]+a[i-1][j-1],j>1
In this way, when fixed in column I to column J, the conversion into a one-dimensional array of the K element can be expressed as f[k][j]-f[k][i-1],k is the subscript, I and J are column subscript, because the matrix statistics are from 1, so i,j,k is greater than or equal to 1, that is, i-1 will not subscript out of bounds, This is the role of the Front Sentinel.
The solution recurrence formula for fixed I and J columns:
· P[N+1],P[K] represents the fixed column I to column J, from line 1th to the range K, including the largest sub-matrix of line K.
· Q[n+1],q[k] represents the maximum sub-matrix when the column I is fixed to column J, from line 1th to the K-line range.
P[k]=f[0][j]-f[0][i-1], k=1
P[k]=p[k-1]>0? (P[k-1]+f[k][j]-f[k][i-1]):(f[k][j]-f[k][i-1]), k>1
Q[k]=p[k], k=1
Q[k]=max{q[k-1], P[k]}, k>1
Q[n] is the maximum and within the range of fixed columns (I-J).

#include <iostream> #include <vector> #include <math.h>using namespace Std;//int maxval_temp (int* A, int n) {if (n==1) return a[n-1];int maxall=a[n-1];int maxstart=a[n-1];for (int i=n-2;i>=0;i--) {Maxstart=max (a[i],a[i ]+maxstart); Maxall=max (Maxall,maxstart);} return maxall;} int Maxval (vector<vector<int>> vec) {if (Vec.empty ()) return 0;int m,n;m=vec.size (); n=vec[0].size (); if (m= =1) return vec[0][0];int max_val=vec[0][0];for (int i=0;i<m;i++) for (int j=i;j<m;j++) {int* ptr=new int[n];for (int k=0;k<n;k++) {int temp1=0;for (int g=i;g<j;g++) temp1+=vec[k][g];p tr[k]=temp1;} int temp_max=maxval_temp (ptr,n); if (Temp_max>max_val) Max_val=temp_max;} return max_val;} int main () {}

  

The beauty of programming-the maximum value of the sum of the words group (two-dimensional, dynamic programming)

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.