(最大子矩阵和)给出m行n列的整数矩阵,求最大的子矩阵和(子矩阵不能为空)。
输入第一行包含两个整数m和n,即矩阵的行数和列数。之后m行,每行n个整数,描述整个矩阵。程序最终输出最大的子矩阵和。(最后一空4分,其余3分,共16分)
const
SIZE=100;
var
matrix: array [1..SIZE, 1..SIZE] of integer;
rowsum: array [1..SIZE, 0..SIZE] of integer;
//rowsum[i, j]记录前i行前j个数的和
M,n, i, j, first, last, area, ans:integer;
begin
read(m, n);
for i := 1 to m do
for j:=1 to n do
read(matrix[i, j]);
ans:=matrix [1,1] ;
for i:=1 to m do
rowsum[i,0]:=0 ;
for i:=1 to m do
for j:=1 to n do
rowsum[i, j]:=_ rowsum[i,j-1]+matrix[i,j] ;
for first := 1 to n do
for last:=first to n do
begin
area:=0 ;
for i:=1 to m do
begin
area:=area+ rowsum[i,last]-rowsum[i,first-1] ;
if (area>ans) then
ans:=area;
if (area<0) then
area:=0;
end;
end;
writeln(ans);
end.