标签:i++ return sha main lan row 内存 来源 ble
4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
15
分析:
先对每一行计算前缀和数组,用于方便地计算每一行指定段的元素之和。
然后枚举子矩阵的起始列first、结束列last。然后在这个区域计算列数为last-first+1的所有子矩阵的最大和。(计算过程类似一维矩阵的最大子段和。)
1 #include <stdio.h> 2 const int SIZE = 100; 3 int matrix[SIZE + 1][SIZE + 1]; 4 int rowsum[SIZE + 1][SIZE + 1]; //rowsum[i][j]记录第 i 行前 j 个数的和 5 int m, n, i, j, first, last, area, ans; 6 int main() 7 { 8 scanf("%d",&n); 9 m=n; 10 for (i = 1; i <= m; i++) 11 for (j = 1; j <= n; j++) 12 scanf("%d", &matrix[i][j]); 13 ans = matrix[1][1]; 14 for (i = 1; i <= m; i++) 15 rowsum[i][0]=0; 16 for (i = 1; i <= m; i++) 17 for (j = 1; j <= n; j++) 18 rowsum[i][j] = rowsum[i][j-1]+matrix[i][j]; 19 for (first = 1; first <= n; first++) 20 for (last = first; last <= n; last++) 21 { 22 area=0; 23 for (i = 1; i <= m; i++) 24 { 25 area += rowsum[i][last] -rowsum[i][first-1]; 26 if (area > ans) ans = area; 27 if (area < 0) area = 0; 28 } 29 } 30 printf("%d\n", ans); 31 return 0; 32 }
标签:i++ return sha main lan row 内存 来源 ble
原文地址:http://www.cnblogs.com/huashanqingzhu/p/6755084.html