1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include <stdio.h> int a[100][100]={0}; int N; int Line[100]; int KadaneAlg(); int MatSum(int a,int b); int main() { int p=-100; int Max=-100; while(scanf("%d",&N)==1) { Max=-100; p =-100; for(int i=0;i<N;i++) { for(int j=0;j<N;j++) scanf("%d",&a[i][j]); } for(int k=0;k<N;k++) { for(int l=k;l<N;l++) { p = MatSum(k,l); if(p>Max) Max = p; } } printf("%d\n",Max); } return 0; } int MatSum(int c,int b) { int p; for(int i=0;i<N;i++) { p =0; for(int j=c;j<=b;j++)//row { p += a[j][i]; } Line[i] = p; } return KadaneAlg(); } int KadaneAlg() { int max_so_far = 0, max_ending_here = 0; int i; for(i = 0; i < N; i++) { max_ending_here = max_ending_here + Line[i]; if(max_ending_here < 0) max_ending_here = 0; else if (max_so_far < max_ending_here) max_so_far = max_ending_here; } return max_so_far; } |