经典的最大子矩阵和。。
转化成最大子段和
(通过枚举列的长度,然后对于选定的那一列做一次的最长子段和)最后选取最大就是答案。。
1 /* 2 State:Accepted 3 Time:2013.3.1 4 */ 5 6 #include <iostream> 7 #include <cstdlib> 8 #include <cstdio> 9 #include <cstring> 10 #include <algorithm> 11 #include <fstream> 12 #include <string> 13 #include <cmath> 14 using namespace std; 15 int n , a[150][150] ,f[200], sumrow[200][200] , ans ; 16 17 void init(){ 18 scanf("%d",&n); 19 for (int i = 1; i <= n; ++i) 20 for (int j = 1; j <= n; ++j){ 21 scanf("%d",&a[i][j]); 22 sumrow[i][j] = sumrow[i - 1][j] + a[i][j]; 23 } 24 25 } 26 27 void dp(){ 28 int nowv; 29 ans = -1000000; 30 for (int l = 1; l <= n; ++l) 31 for (int r = l; r <= n; ++r){ 32 for (int i = 1; i <= n; ++i) f[i] = -1000000; 33 for (int i = 1; i <= n; ++i){ 34 nowv = sumrow[r][i] - sumrow[l - 1][i]; 35 f[i] = max(nowv , f[i -1] + nowv); 36 ans = max(ans , f[i]); 37 } 38 } 39 printf("%d\n",ans); 40 41 } 42 int main(){ 43 freopen("poj1050.in","r",stdin); 44 freopen("poj1050.out","w",stdout); 45 init(); 46 dp(); 47 fclose(stdin); 48 fclose(stdout); 49 }