Jeffrey&Lynny

一个温馨小家庭的.Net生活

导航

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.

posted on 2008-07-06 10:54  比尔盖房  阅读(298)  评论(0编辑  收藏  举报