# 【DP】数字三角形4

数字三角形4
 描述 Description 数字三角形必须经过某一个点，使之走的路程和最大
 输入格式 Input Format 第1行n，表示n行 <=25 第2到n+1行为每个的权值 第n+2行为两个数x,y表示必须经过的点
 输出格式 Output Format 最大值
 样例输入 Sample Input [复制数据] 211 11 1
 样例输出 Sample Output [复制数据] 2
 时间限制 Time Limitation 各个测试点1s
 注释 Hint 各个测试点1s

========================================

2个DP

===========================

var
n:longint;
map:array[1..25,1..25]of longint;
f:array[0..26,0..26]of longint;
x,y:longint;
ans1,ans2:longint;
procedure init;
begin
assign(input,'ty1084.in');
assign(output,'ty1084.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 work1;
var
i,j:longint;
begin
f[1,1]:=map[1,1];
for i:=2 to x do
for j:=1 to i do
begin
f[i,j]:=max(f[i-1,j-1],f[i-1,j])+map[i,j];
end;
ans1:=f[x,y];
end;

procedure work2;
var
i,j:longint;
begin
fillchar(f,sizeof(f),0);
for i:=1 to n do
f[n,i]:=map[n,i];
for i:=n-1 downto x do
for j:=1 to i do
begin
f[i,j]:=max(f[i+1,j],f[i+1,j+1])+map[i,j]
end;
ans2:=f[x,y];
end;

procedure main;
var
i,j:longint;
begin
for i:=1 to n do
for j:=1 to i do
fillchar(f,sizeof(f),0);
ans1:=0; ans2:=0;
work1;
work2;
writeln(ans1+ans2-map[x,y]);
end;

begin
init;
main;
terminate;
end.

{for i:=x-1 downto 1 do
for j:=1 to i do
f[i,j]:=max(f[i+1,j],f[i+1,j+1])+map[i,j];}

• 写评论