动态规划递归和一点记忆化
我在这个三角形中有大量从 0 到 4 的整数。 我正在尝试使用 Ruby 学习动态编程,并且需要一些帮助来计算三角形中满足三个条件的路径数量:
- 您必须从包含 70 个元素的行中的零点之一开始。
- 您的路径可以位于您正上方的一行(如果正上方有数字)或标题对角线左侧的一行。 其中一个选项始终可用。
- 到达第一行零的路径总和必须等于 140。
例如,从底行第二个零开始。 您可以直接向上移动到 1 或 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
I have this massive array of ints from 0-4 in this triangle. I am trying to learn dynamic programming with Ruby and would like some assistance in calculating the number of paths in the triangle that meet three criterion:
- You must start at one of the zero points in the row with 70 elements.
- Your path can be directly above you one row (if there is a number directly above) or one row up heading diagonal to the left. One of these options is always available
- The sum of the path you take to get to the zero on the first row must add up to 140.
Example, start at the second zero in the bottom row. You can move directly up to the one or diagonal left to the 4. In either case, the number you arrive at must be added to the running count of all the numbers you have visited. From the 1 you can travel to a 2 (running sum = 3) directly above or to the 0 (running sum = 1) diagonal to the left.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但我喜欢家庭作业:)
我发现从顶部开始并遵循相反的规则更容易推理“路径”问题。
这意味着:
“和”规则只是选择所有完整路径中的某些。
But I like homework :)
I find it easier to reason about the 'paths' problem when starting from the top, and following the rules the other way around.
This means:
The 'sum' rule just selects certain of all complete paths.