最大加权矩形
题目描述:
给定一个正整数n( n<=100),然后输入一个N*N矩阵。求矩阵中最大加权矩形,即矩阵的每一个元素都有一权值,权值定义在整数集上。从中找一矩形,矩形大小无限制,是其中包含的所有元素的和最大。矩阵的每个元素属于[-127,127]
例:
0 –2 –7 0 在左下角: 9 2
9 2 –6 2 -4 1
-4 1 –4 1 -1 8
-1 8 0 –2 和为15
输入:
第一行:n,接下来是n行n列的矩阵。
输出:
最大矩形(子矩阵)的和。
样例输入:
4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
样例输出:
15
==================================
==========================
var
n:longint;
map:array[1..100,1..100]of longint;
f,sum1:array[0..100]of longint;
sum:array[0..100,0..100]of longint;
procedure init;
begin
assign(input,'a.in');
assign(output,'a.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
function max(a,b:longint):longint;
begin
if a>b then exit(a);
exit(b);
end;
procedure main;
var
i,j:longint;
a,b,c,d:longint;
t:longint;
ans:longint;
min:longint;
begin
readln(n);
fillchar(sum,sizeof(sum),0);
for i:=1 to n do
for j:=1 to n do
begin
read(map[i,j]);
sum[i,j]:=sum[i,j-1]+map[i,j];
//将每一行压为一个点..
end;
ans:=-maxlongint;
for b:=1 to n do
for d:=b to n do
begin
fillchar(sum1,sizeof(sum1),0);
for a:=1 to n do
begin
sum1[a]:=max(sum1[a-1]+sum[a,d]-sum[a,b-1],sum[a,d]-sum[a,b-1]);
if sum1[a]>ans then ans:=sum1[a];
end;
end;
writeln(ans);
end;
begin
init;
main;
terminate;
end.