USACO: Section 1.5 -- PROB Number Triangles

动态规划解决路径问题
本文通过动态规划的方法解决了路径选择问题。介绍了如何从指数级的路径搜索空间减少到多项式级别的搜索空间,避免了重复计算,提高了算法效率。
Source Code 

This problem requires us to search all the possible paths. For every level i, the i+1 level has searching choices: f(i+1)=2*f(i). So, for R rows, the final level paths are O(2^R). This calculation paths number is not acceptable for computer since R can be 1000. We need some way to reduce the paths.

Then, we see the insight. We find that for level i, there should be i paths for decisions. For item (i, j), we can filter other paths to it and just keep one current max sum path. This is just the dynamic programming. We have paths 1+2+3...+R = O(R^2) paths, which is acceptable for calculation now.

Lesson Learned:
1. The difference between dynamic programming and brute-force searching is that there may be some redundant states during the searching. That is, the searching tree has many nodes that are the same node coming from other parent nodes. So, the tree nodes numbers are not that large if we filter the redundant nodes. So, we can dynamically judge in the middle state to filter a lot of search paths. This will greatly improve the searching boundaries.
2. Set code copying operation as an alarm.
3. While looping through a 2-d array, we'd better use indexing varialbe names "row", "item" instead of "i", "j" for readability reason.
4. Always assert the index range before using the index to access array items.

转载于:https://www.cnblogs.com/jeffreytan/archive/2008/07/06/1236819.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值