动态编程递归和一系列Memoization

时间:2009-03-25 21:21:16

标签: recursion dynamic-programming memoization

我在这个三角形中有0到4的大量整数。我正在尝试使用Ruby学习动态编程,并希望在计算符合三个标准的三角形中的路径数量方面提供一些帮助:

  1. 您必须从包含70个元素的行中的一个零点开始。
  2. 你的路径可以直接位于你的上方一行(如果上面有一个数字)或者左上角的一行。其中一个选项始终可用
  3. 您在第一行上获得零的路径总和必须加起来为140。
  4. 示例,从底行的第二个零开始。您可以直接向上移动到左侧的一个或左对角线。在任何一种情况下,您到达的号码必须添加到您访问过的所有号码的运行计数中。从1开始,您可以直接到达上方的2(运行总和= 3)或左侧的0(运行总和= 1)对角线。

    0  
    41  
    302  
    2413  
    13024  
    024130  
    4130241  
    30241302  
    241302413  
    1302413024  
    02413024130  
    413024130241  
    3024130241302  
    24130241302413  
    130241302413024  
    0241302413024130  
    41302413024130241  
    302413024130241302  
    2413024130241302413  
    13024130241302413024  
    024130241302413024130  
    4130241302413024130241  
    30241302413024130241302  
    241302413024130241302413  
    1302413024130241302413024  
    02413024130241302413024130  
    413024130241302413024130241  
    3024130241302413024130241302  
    24130241302413024130241302413  
    130241302413024130241302413024  
    0241302413024130241302413024130  
    41302413024130241302413024130241  
    302413024130241302413024130241302  
    2413024130241302413024130241302413  
    13024130241302413024130241302413024  
    024130241302413024130241302413024130  
    4130241302413024130241302413024130241  
    30241302413024130241302413024130241302  
    241302413024130241302413024130241302413  
    1302413024130241302413024130241302413024  
    02413024130241302413024130241302413024130  
    413024130241302413024130241302413024130241  
    3024130241302413024130241302413024130241302  
    24130241302413024130241302413024130241302413  
    130241302413024130241302413024130241302413024  
    0241302413024130241302413024130241302413024130  
    41302413024130241302413024130241302413024130241  
    302413024130241302413024130241302413024130241302  
    2413024130241302413024130241302413024130241302413  
    13024130241302413024130241302413024130241302413024  
    024130241302413024130241302413024130241302413024130  
    4130241302413024130241302413024130241302413024130241  
    30241302413024130241302413024130241302413024130241302  
    241302413024130241302413024130241302413024130241302413  
    1302413024130241302413024130241302413024130241302413024  
    02413024130241302413024130241302413024130241302413024130  
    413024130241302413024130241302413024130241302413024130241  
    3024130241302413024130241302413024130241302413024130241302  
    24130241302413024130241302413024130241302413024130241302413  
    130241302413024130241302413024130241302413024130241302413024  
    0241302413024130241302413024130241302413024130241302413024130  
    41302413024130241302413024130241302413024130241302413024130241  
    302413024130241302413024130241302413024130241302413024130241302  
    2413024130241302413024130241302413024130241302413024130241302413  
    13024130241302413024130241302413024130241302413024130241302413024  
    024130241302413024130241302413024130241302413024130241302413024130  
    4130241302413024130241302413024130241302413024130241302413024130241  
    30241302413024130241302413024130241302413024130241302413024130241302  
    241302413024130241302413024130241302413024130241302413024130241302413  
    1302413024130241302413024130241302413024130241302413024130241302413024  
    02413024130241302413024130241302413024130241302413024130241302413024130  
    

1 个答案:

答案 0 :(得分:1)

但我喜欢作业:)

我发现从顶部开始时更容易推理出“路径”问题,并且反过来遵循规则。

这意味着:

  • 部分路径可以是前零点,也可以是扩展的部分路径
  • 部分路径Pr,c的扩展,除非r是最后一行,它们是完整的,
    • Pr,c + P(r + 1),c
    • 的扩展
    • Pr,c + P(r + 1),c + 1
    • 的扩展

'sum'规则只选择所有完整路径中的某些路径。