#include <stdio.h>
#define MAX_SIZE 100
#define INF 1e7
int size;
int matrix[MAX_SIZE][MAX_SIZE];
int sumOfPreRowsMatrix[MAX_SIZE][MAX_SIZE];
int array[MAX_SIZE];
int maxSubArray(){
int maxSum = -INF;
int tempSum = 0;
int i;
for (i = 0; i < size; i++){
tempSum += array[i];
if (tempSum > maxSum)
maxSum = tempSum;
if (tempSum < 0)
tempSum = 0;
}
return maxSum;
}
int main(){
scanf("%d", &size);
int row, column;
for (row = 0; row < size; row++)
for (column = 0; column < size; column++){
scanf("%d", &matrix[row][column]);
sumOfPreRowsMatrix[row][column] = matrix[row][column];
}
for (row = 0; row < size; row++)
for (column = 0; column < size; column++){
if (row == 0)
continue;
sumOfPreRowsMatrix[row][column] += sumOfPreRowsMatrix[row - 1][column];
}
int maxSubMatrix = -INF;
int startRow, endRow;
for (startRow = 0; startRow < size; startRow++)
for (endRow = startRow; endRow < size; endRow++){
for (column = 0; column < size; column++)
if (startRow == 0)
array[column] = sumOfPreRowsMatrix[endRow][column];
else
array[column] = sumOfPreRowsMatrix[endRow][column] - sumOfPreRowsMatrix[startRow - 1][column];
int maxSum = maxSubArray();
if (maxSum > maxSubMatrix)
maxSubMatrix = maxSum;
}
printf("%d\n", maxSubMatrix);
return 0;
}
URAL 1146 Maximum Sum (最大子矩阵)
最新推荐文章于 2018-05-07 21:02:51 发布