/ THE IDEA
Define a smaller problem precisely: what is the greatest reward obtainable upon reaching this square? Any route reaching an interior square must arrive from the square immediately above or immediately left. Compare the best rewards already known for those two predecessors, then add the current reward. The move restriction makes an evaluation order possible. Fill the top row and left column first, then work across successive rows. Each answer depends only on answers already available. A table entry summarises all earlier route choices; it need not store every route separately.
THE FORMAL IDEA
B(r,c) = v(r,c) + max(B(r−1,c), B(r,c−1))
| B(r,c) = best total at row r, column c; v(r,c) = reward on that square. | | max selects the larger predecessor total. Start with the top-left reward; on the top row or left edge there is only one permitted predecessor. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
Rewards are top row [1, 2], bottom row [4, 1]. The best totals are top row [1, 3], bottom row [5, 6]. The final choice compares 3 from above with 5 from the left, then adds 1. Change the top-right reward from 2 to 8: totals become [1, 9] and [5, 10].
|
The best journey changes from down-then-right to right-then-down. The procedure does not cling to a previous route; it recomputes the affected summary and lets that change propagate to the final answer.
/ SO WHAT?
This is more than caching arbitrary calculations. The smaller answers must contain everything needed for later decisions. If collecting a special key changes which later doors can open, position alone is no longer enough: the stored situation must also record whether the key has been collected.
ONE CAVEAT |
| This example has no revisiting and an additive reward. Cycles, limits on repeated rewards or hidden dependencies require a different state definition. Storing too little gives wrong answers; storing too much can make the table prohibitively large. |
KEEP THIS
Reuse smaller answers only after defining a state that preserves every past detail capable of changing future choices.
|
NEXT: Why mixing coloured lights differs from mixing filters
|