经典DP,一张纸条从A传给B,另一张从B传给A和两张纸条同时从A传给B是一样的,设f[x1,y1,x2,y2]为第一张纸条到达(x1,y1),第二张纸条到达(x2,y2)时所得的最大值,设x为max(f[x1-1,y1,x2-1,y2],f[x1-1,y1,x2,y2-1],f[x1,y1-1,x2-1,y2],f[x1,y1-1,x2,y2-1])则有:
if (x1=0) or (x2=0) or (y1=0) or (y2=0) then f[x1,y1,x2,y2]:=0
if (x1=x2) and (y1=y2) then f[x1,y1,x2,y2]:=x+a[x1,y1]
if (x1<>x2) and (y1<>y2) then f[x1,y1,x2,y2]:=x+a[x1,y1]+a[x2,y2]
程序:
program message;
var
x:longint;
m,n,i,j,x1,x2,y1,y2:integer;
a:array[1..50,1..50] of longint;
f:array[0..51,0..51,0..51,0..51] of longint;
function max(a1,a2,a3,a4:longint):longint;
var
p,q:longint;
begin
if a1>a2 then p:=a1
else p:=a2;
if a3>a4 then q:=a3
else q:=a4;
if p>q then max:=p
else max:=q;
end;
begin
assign(input,'message.in');
reset(input);
assign(output,'message.out');
rewrite(output);
fillchar(a,sizeof(a),0);
fillchar(f,sizeof(f),0);
readln(n,m);
for i:=1 to n do
begin
for j:=1 to m do
read(a[i,j]);
readln;
end;
for x1:=1 to n do
for y1:=1 to m do
for x2:=1 to n do
for y2:=1 to m do
begin
x:=max(f[x1-1,y1,x2-1,y2],f[x1-1,y1,x2,y2-1],f[x1,y1-1,x2-1,y2],f[x1,y1-1,x2,y2-1]);
if (x1=x2) and (y1=y2) then f[x1,y1,x2,y2]:=x+a[x1,y1]
else f[x1,y1,x2,y2]:=x+a[x1,y1]+a[x2,y2];
end;
writeln(f[n,m,n,m]);
close(input);
close(output);
end.