最大子矩阵和问题是对最大子序列和问题的扩展,即从一维扩展到了二维。但是解决此问题的方法和原来的方法并没有太大的差别,这里就以同样的动态规划的实录思路来求解此问题。原来subSum[i]代表包含ai并且以ai结束的子序列的最大和,状态转移方程为subSum[i+1] = subSum[i]<0 ? 0 : subSum[i]+a[i+1],因为subSum[i]为负将对总和做负贡献,所以此时将其丢弃。而现在,序列已经变成了矩阵,subSum[i]也不能再表示原来的意义了,需要将其扩展为二维,subSum[k]表示包含第k行第i至j列元素并且以其结尾的子矩阵的最大和,定义sum[i,j]表示表示第k行第i至j列元素和,那么同样的有转移方程subSum[k+1] = subSum[k]<0 ? 0 : subSum[k]+sum[i,j],最后的问题就是如何计算sum[i,j],因为已经有三层循环枚举i、j、k了,如果这里再用循环计算,复杂度将会增加,所以可以在输入数据的时候将sum[i,j]处理成第i行前j列的和,所以原来转移方程中的sun[i,j]就可以直接写成sum[k,j]-sum[k,i-1],不用再循环计算了。实现代码如下:
#define MAX 105
// 第i行前j个元素和
int sum[MAX][MAX];
// 矩阵的行列数
int n;
// 返回最大子矩阵和,并且保存该子矩阵的起始元素和结束元素的行列值
int MaxSubMatSum(int &sX, int &sY, int &eX, int &eY)
{
int ans=1e-6;
// 枚举第k行,第i列至j列的和
for(int i=1; i<=n; ++i)
{
for(int j=i; j<=n; ++j)
{
int subSum = 0;
for(int k=1; k<=n; ++k)
{
// 动态规划思想,subSum<0将会减少总和,所以此时将其置0
if(subSum < 0)
{
subSum = 0;
sX = k;
sY = i;
}
subSum += sum[k][j] - sum[k][i-1];
if(ans < subSum)
{
ans = subSum;
eX = k;
eY = j;
}
}
}
}
return ans;
}
以一道题为例,应用上述算法,HDOJ:1081,时空转移( 点击打开链接),题目如下:
To The Max
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8952 Accepted Submission(s): 4328
Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
Input
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
Output
Output the sum of the maximal sub-rectangle.
Sample Input
4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
Sample Output
15
给出一个n*n的矩阵,找出其最大子矩阵和,只需要输出和即可。
分析:
典型的计算最大子矩阵和的题目,如前面分析求解即可。但是不需要输出子矩阵本身,所以可以对前面的算法简化,直接写入主程序即可。
源代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 105
// 第i行前j个元素和
int sum[MAX][MAX];
int main()
{//freopen("sample.txt", "r", stdin);
int n;
while(~scanf("%d", &n))
{
int data, ans=1e-6;
memset(sum, 0, sizeof(sum));
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=n; ++j)
{
scanf("%d", &data);
// 累加第i行前j个元素和
sum[i][j] += sum[i][j-1] + data;
}
}
// 枚举第k行,第i列至j列的和
for(int i=1; i<=n; ++i)
{
for(int j=i; j<=n; ++j)
{
int subSum = 0;
for(int k=1; k<=n; ++k)
{
// 动态规划思想,subSum<0将会减少总和,所以此时将其置0
if(subSum < 0) subSum = 0;
subSum += sum[k][j] - sum[k][i-1];
ans = max(ans, subSum);
}
}
}
printf("%d\n", ans);
}
return 0;
}