#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) 编辑 收藏 引用 所属分类:
DP 、
Algorithm