posts - 2,  comments - 3,  trackbacks - 0

#include<cstdio>

int numberMatrix[101][101];
int subNumberSum[101][101];
int numbers[101];
int n;

int maxSubSum(int* numbers, int n){//计算最大子序列和
      int i = 0;
      int sum = 0;
      int maxSum = numbers[0];
      for(i = 0; i < n; i++){
         sum += numbers[i];
         if(sum > maxSum){
            maxSum = sum;
         }
         if(sum < 0){
            sum = 0;
         }
      }

      return maxSum;
}

int maxSubMatrixSum(int numberMatrix[101][101], int n){//计算最优子矩阵
      int i = 0;
      int j = 0;
      int k = 0;
      int tempSum = 0;
      int maxSum = 0;
      for(i = 1; i <= n; i++){//初始化第零行,全为0
         subNumberSum[0][i] = 0;
      }
      for(i = 1; i <= n; i++){//计算subNumberSum
            for(j = 1; j <= n; j++){
                  subNumberSum[i][j] = subNumberSum[i - 1][j] + numberMatrix[i][j];
            }
      }
      maxSum = subNumberSum[1][1];
      for(i = n; i >= 1; i--){//枚举计算maxSubSum
            for(j = i - 1; j >= 0; j--){
                  for(k = 1; k <= n; k++){
                        numbers[k - 1] = subNumberSum[i][k] - subNumberSum[j][k];
                  }
                  tempSum = maxSubSum(numbers, n);
                  if(tempSum > maxSum){
                        maxSum = tempSum;
                  }
            }
       }

       return maxSum;
}

int main(int argc, char* argv[], char* env[])
{
      int i = 0;
      int j = 0;
      while(scanf("%d", &n) != EOF){
            for(i = 1; i <= n; i++){
                  for(j = 1; j <= n; j++){
                         scanf("%d", &numberMatrix[i][j]);
                  }
            }
            printf("%d\n", maxSubMatrixSum(numberMatrix, n));
      }

      return 0;
}

posted on 2011-07-20 08:58 Lshain 阅读(372) 评论(0)  编辑 收藏 引用 所属分类: DPAlgorithm

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿

文章分类(46)

文章档案(33)

ACM

Algorithm Link

BLOG

Format analysis

Forum

Math

mirror

OpenGL

Protocol Analyzer

Recent Contests

Search

WIN32 Programming

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜