Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1050
The meaning of the question is very simple. Find its sub-matrix in a matrix to maximize the sum of the sub-Matrix Values. In fact, it is the promotion of the largest sub-segment and problem in two-dimensional space. Let's talk about the one-dimensional situation first: It has arrays A0, A1... An. Find the consecutive sub-segments to maximize the sum of them. Assume that for sub-segments: 9 2-16 2 temp [I], it indicates the maximum sub-segment and in the sub-segment ending with AI. If temp [I] is known, the method for obtaining temp [I + 1] is:
If temp [I]> 0 temp [I + 1] = temp [I] + AI (add AI to the previous child segment ), otherwise, temp [I + 1] = AI (without the preceding sub-segment), that is, the state transition equation:
Temp [I] = (temp [I-1]> 0? Temp [I-1]: 0) + Buf [I];
For the example temp: 9 11-5 2, then the largest sub-segment in temp [] is the largest of the one-dimensional sequence. Evaluate the functions of the largest subsegment and in one dimension:
Int getmax (INT Buf [100], int N)
{
Int temp [101], max = N * (-127 );
Memset (temp, 0, 4 * (n + 1 ));
For (INT I = 1; I <= N; I ++)
{
Temp [I] = (temp [I-1]> 0? Temp [I-1]: 0) + Buf [I];
If (max <temp [I])
Max = temp [I];
}
Return Max;
}
The following is an example of two-dimensional scaling:
0-2-7 0
9 2-6 2
-4 1-4 7
-1 8 0-2
We use I j to indicate the start and end rows respectively, and traverse all possibilities:
For (I = 1; I <= N; I ++)
For (j = I; j <= N; j ++ ){}
In one of the cases, I = 2 J = 4, which is equivalent to selecting 2 3 4 and 3 rows, so that the combination of those columns can obtain the maximum value, because it is always 2 3 4 rows, so we can "bundle" these three lines and convert them to 4 ( 9-4-1 ), 11 (8 + 2 + 1),-10 (-6-4 + 0), 7 (7 + 2-2), and OK, the problem is successfully converted to a one-dimensional problem!
Code with detailed comments can be obtained at http://download.csdn.net/user/china8848/