/*题意是求最大子矩阵,表示线代没学好,好在不影响这道题。这题的主要思路就是二维化成一维。把二维数组前一列的元素累加到后边一列上,例如:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
处理后就是:
0 -2 -9 -9
9 11 5 7
-4 -3 -7 -6
-1 7 7 5
然后就是
dp[i][j] = max(dp[i][j-1], 0) + sum[j];
My Code:*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int N = 110;
const int inf = 0x7fffffff;
int sum[N][N];
int main() {
//freopen("data.in", "r", stdin);
int n, i, j, x, max, flag, k;
while(~scanf("%d", &n)) {
memset(sum, 0, sizeof(sum));
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
scanf("%d", &x);
sum[i][j] = sum[i][j-1] + x;
}
}
max = -inf;
for(i = 1; i <= n; i++) {
for(j = 1; j <= i; j++) {
for(flag = 0, k = 1; k <= n; k++) {
flag += sum[k][i] - sum[k][j-1];
if(flag > max) max = flag;
if(flag < 0) flag = 0;
}
}
}
printf("%d\n", max);
}
return 0;
}
转载于:https://www.cnblogs.com/vongang/archive/2011/11/05/2237235.html