Suppose I have an n-high pyramid of numbers like:
1
5 8
2 5 4
8 9 3 1
2 8 3 7 2
How can I algorithmically walk through every possible path from the top of the pyramid to the bottom?
To better explain, let me represent the pyramid this way:
1 2 3 4 5
-----------
1 | a
5 8 | b
2 5 4 | c
8 9 3 1 | d
2 8 3 7 2 | e
For example:
On the path | a b c d e (Path)
-------------|------------------
Path 1: 1 5 2 8 2 | 1 1 1 1 1
Path 2: 1 5 2 8 8 | 1 1 1 1 2
Path 3: 1 5 2 9 8 | 1 1 1 2 2
Path 4: 1 5 2 9 3 | 1 1 1 2 3
...
Path m: 1 8 4 1 2 | 1 2 3 4 5
Where m is the total number of paths.
I understand that the problem can be reduced to simply generate all the possible paths denoted in the second column of the table, and I could come up fairly easily with an algorithm to generate those for a 5-high (or any other fixed high) pyramid, but I can't seem to come up with a general solution.
The algorithm cannot be recursive, for the simple reason that I've already solved the problem recursively and I'm interested in a non-recursive solution to get a complete view of the problem.