Dynamic Programming Practice Questions
These questions are NOT to turn in; they are just for your practice.
(This is similar to the “sticks” problem presented in class.)
Consider a two-
player game in which players make consecutive moves.
There are 21 sticks in a pile.
each turn, a player may take one, two, four, or seven sticks from any one pile.
takes the last stick loses the game.
Use dynamic programming to determine from which states you can win this game, and
from which states you can force your opponent to lose.
you know that if there’s 1
stick left, you will lose.
So “1 stick remaining” is a losing state.
Can you figure out
some easy winning states from there?
Continue working backwards until you get to 21.
of the game be the number of remaining sticks.
If there is one stick left,
then we have lost the game.
Therefore, states 2, 3, and 5 are winning states, because we
can take 1, 2, and 4 sticks, respectively, and put our opponent in state 1.
So far, we have
Continue to work backwards.
We now know what happens in states 1, 2, and 3.
happens in state 4?
We can either take 1, 2, or 4 sticks.
If we take 1 stick, our opponent
is in a winning state (State 3), and so we lose.
If we take 2 sticks, our opponent is in
State 2, and we lose.
Taking all four sticks is a loss because we’d take the last stick.
matter what we do, we lose if there are four sticks left, and so State 4 is a losing state.
But since state 4 is a loss, then states 5, 6, and 8 must be wins:
from these states, we
know how to put our opponent in state 4, which we already know is a loss.
know two ways of defeating our opponent if we’re in state 5!)
This is what we’ve got:
The next question mark is at state 7.
We can either take one stick (opponent gets state 6),
two sticks (opponent gets state 5), or four sticks (opponent gets state 3).
Either way, the