The question is simple: calculate the maximum value of a matrix neutron matrix element.
Idea: Let's look at any two rows.Column andCalculates the maximum child segment. Maximum subsegment recursive equation: s [k] = (s [k-1]> 0 )? (S [k-1] + A [k]): A [K]. S [k] indicates the maximum child segment sum ending with a [K,
The equation means that if s [k-1]> 0, the maximum child segment ending with a [k] includes the child segment above, otherwise, the maximum child segment ending with a [k] is itself.
Code:
# Include < Cstdio >
Using Namespace STD;
Int N;
Int Matrix [ 100 ] [ 100 ];
Int S [ 100 ];
Int Get_max (){
Int Max = - 127 * 100 * 100 ;
For ( Int I = 0 ; I < N; I ++ )
For ( Int J = I; j < N; j ++ ){
Int A = 0 ;
For ( Int X = I; x <= J; x ++ )
A + = Matrix [x] [ 0 ];
S [ 0 ] = A;
If (S [ 0 ] > Max)
Max = S [ 0 ];
For ( Int K = 1 ; K < N; k ++ ){
A = 0 ;
For ( Int X = I; x <= J; x ++ )
A + = Matrix [x] [k];
If (S [K - 1 ] > 0 )
S [k] = S [K - 1 ] + A;
Else
S [k] = A;
If (S [k] > Max)
Max = S [k];
}
}
Return Max;
}
Int Main (){
// Freopen ("in", "r", stdin );
While (Scanf ( " % D " , & N) ! = EOF ){
For ( Int I = 0 ; I < N; ++ I)
For ( Int J = 0 ; J < N; ++ J)
Scanf ( " % D " , & Matrix [I] [J]);
Int Max;
Max = Get_max ();
Printf ( " % D \ n " , Max );
}
}
S [k] = (s [k-1]> 0 )? (S [k-1] + a): As [k] = (s [k-1]> 0 )? (S [k-1] + a):