Analysis we have solved one-dimensional problems (the largest sub-segments and problems in the foundation), and now we are two-dimensional, and we see if we can turn this problem into one-dimensional problem. The last sub-matrix must be between some two rows. Suppose we think that the sub-matrix is between line I and column J, how do we get I and J, yes, enum. Enumerates all 1<=i<=j<=m, representing the row range selected by the final sub-matrix. We take each row from line I to line J and find out, form an array C, and then a row I to line J between the largest sub-matrix and corresponding to this and the largest child of the array C and. As a result, our algorithm becomes:
For i = 1 to m dofor j = i to M do//calculates the first row I to column j and for k = 1 to N doc[k] = (j = = i)? A[i][k]: (C[k] + a[j][k]) endfor The maximal sub-segments of C and the global optimal results of endforendfor are obtained.
Let's look at the marked red part of the sum of all the numbers between rows I and J, and we don't use a loop again, but as J grows, each time the result of line J is superimposed on the previous sum. In addition, the largest subarray of C and a linear time algorithm can actually be combined with that K's for loop, but without affecting the complexity of the time. The complexity of Time is O (m^2n). Finally, we provide input and output data, you write a program, the implementation of this algorithm, only write the correct program, to continue the course behind.
input
Line 1th: M and N, separated by spaces (2 <= m,n <= 500). 2-n + 1 lines: The elements in the matrix, the number of m per line, separated by a space in the middle. ( -10^9 <= m[i] <= 10^9)
Output
The maximum value of the output and. If all the numbers are negative, the output is 0.
Input Example
3 3-1 3-12-1 3-3 1 2
Sample Output
7
Dynamic planning [Getting Started]1-max Sub-matrices and