POJ1050 To the MAX question
Question
Give a matrix of N * N to find a child matrix to maximize the sum of the Child matrix. (N <= 100)
Ideas
One-dimensional situations are classic "Maximum continuity and problems ". We want to reduce the problem of two dimensions to one dimension. We enumerate the highest and lowest layers, add the values in the middle to a tmp array, and then use the tmp array to "Maximum continuity and problems" to constantly update ans. The resulting ans must be the largest submatrix.
Code
# Include
# Include
Using namespace std; const int INF = 1000000000; const int maxn = 110; int n; int s [maxn] [maxn]; int tmp [maxn]; int dp [maxn]; int main () {scanf ("% d", & n); for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) scanf ("% d", & s [I] [j]);} int ans =-INF; for (int I = 0; I <n; I ++) {for (int j = I; j <n; j ++) {memset (tmp, 0, sizeof (tmp )); for (int t = I; t <= j; t ++) {for (int c = 0; c <n; c ++) tmp [c] + = s [t] [c];} // for tmp dp ans = max (ans, tmp [0]); int temp = tmp [0]; for (int I = 1; I <n; I ++) {if (temp <= 0) {temp = tmp [I];} else {temp + = tmp [I];} ans = max (ans, temp) ;}} printf ("% d \ n", ans); return 0 ;}