Question:Returns the largest submatrix from a n * n matrix.
Ideas:Returns the maximum continuous sum. We can convert two-dimensional data into one-dimensional data for calculation. Sum [I] [J] indicates the sum of a matrix with (1, 1) AND (I, j) as diagonal. Then, you only need to enumerate each possible value one by one and save the maximum value..
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int INF = 0x3f3f3f; const int maxn = 105; int arr [maxn] [maxn], sum [maxn] [maxn], s [maxn] [maxn]; int main () {int N; while (scanf ("% d", & N )! = EOF) {memset (S, 0, sizeof (s); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {scanf ("% d", & arr [I] [J]); s [I] [J] = s [I] [J-1] + arr [I] [J];} memset (sum, 0, sizeof (SUM )); for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) sum [I] [J] = sum [I-1] [J] + s [I] [J];} int max =-INF; For (INT I = 1; I <= N; I ++) {for (Int J = 0; j <I; j ++) {int T, min; min = 0; for (int K = 1; k <= N; k ++) {T = sum [I] [k]-sum [J] [k]-min; if (T> MAX) max = T; If (sum [I] [k]-sum [J] [k] <min) min = sum [I] [k]-sum [J] [k] ;}} printf ("% d \ n", max);} return 0 ;}