(最大子矩阵和)给出 m行n列的整数矩阵,求最大的子矩阵和(子矩阵不能为空)。
输入第一行包含两个整数m和 n,即矩阵的行数和列数。之后m行,每行 n个整数,描述整个矩阵。程序最终输出最大的子矩阵和。(第一空2 分,其余3分,共 14分)
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 + wsum[i,last]-rowsum[i,first-1] ;
if (area > ans) then
ans := area;
if (area < 0) then
area := 0;
end;
end;
writeln(ans);
end.