Expanding from a one-dimensional array to a two-dimensional matrix is a wonderful process ......
The method of the Two-dimensional matrix is to use the method of the one-dimensional array, that is, to enumerate any two rows, and then find the sum of elements in the same column between the two rows, and then use the method of the one-dimensional array, the maximum continuous sum is the maximum submatrix sum of the current enumeration.
Of course, the idea is to think about how to implement it. It is also a good implementation, that is, I have been confused by the sum [x] [Y] mentioned above, and I have never known how to use it, I also thought about how to use it, but it is useless to use it. The time efficiency is almost the same as that obtained by enumeration.
Paste the Code:
# Include <stdio. h> # include <string. h >#include <iostream >#include <string> using namespace STD; const int maxn = 10; const int INF = 1 <30; int N; int m; int A [maxn] [maxn]; int s [maxn] [maxn]; int C [maxn]; int T [maxn]; int maxsum (int n, int * B) {for (INT I = 1; I <= N; I ++) {T [I] = B [I]-B [I-1];} int ans = 0; int sum = T [1]; for (INT I = 2; I <= N; I ++) {If (sum> 0) {sum + = T [I];} else {sum = T [I];} If (ANS <sum) {Ans = sum ;}} return ans;} int main () {While (scanf ("% d", & N, & M )! = EOF) {memset (S, 0, sizeof (s); For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {scanf ("% d", & A [I] [J]) ;}} for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {s [I] [J] = s [I-1] [J] + s [I] [J-1]-s [I-1] [J-1] + A [I] [J] ;}} int sum =-INF; For (INT I = 1; I <= N; I ++) {for (Int J = I; j <= N; j ++) {c [0] = 0; For (int K = 1; k <= m; k ++) {c [k] = s [J] [k]-s [I-1] [k];} int ans = maxsum (M, C ); if (ANS> sum) {sum = ans ;}}/* For (INT I = 1; I <= N; I ++) {memset (C, 0, sizeof (c); For (Int J = I; j <= N; j ++) {C [0] = 0; For (int K = 1; k <= m; k ++) {C [k] + = A [J] [k];} int ans = maxsum (M, C ); if (ANS> sum) {sum = ans ;}}// this method can also be equivalent to the sum when enumeration columns are obtained. */printf ("% d \ n", sum);} system ("pause"); Return 0 ;}