【DP】数字三角形4

  数字三角形4
描述 Description 
 数字三角形必须经过某一个点,使之走的路程和最大
   
   
 输入格式 Input Format 
 第1行n,表示n行 <=25
第2到n+1行为每个的权值
第n+2行为两个数x,y表示必须经过的点
   
   
  输出格式 Output Format 
 最大值
   
   
  样例输入 Sample Input [复制数据] 
 
   
   
  样例输出 Sample Output [复制数据] 
 
   
   
  时间限制 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
  readln(n);
  for i:=1 to n do
    for j:=1 to i do
      read(map[i,j]);
  readln(x,y);
  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];}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值