Dynamic Programming Practice Questions
and Answers
These questions are NOT to turn in; they are just for your practice.
Question 1.
(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.
At
each turn, a player may take one, two, four, or seven sticks from any one pile.
Whoever
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.
Hint:
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.
Let the
state
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
the following:
State 1
Loss
State 2
Win
State 3
Win
State 4
?
State 5
Win
State 6
?
…
Continue to work backwards.
We now know what happens in states 1, 2, and 3.
What
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.
No
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.
(We now
know two ways of defeating our opponent if we’re in state 5!)
This is what we’ve got:
State 1
Loss
State 8
Win
State 2
Win
State 9
?
State 3
Win
…
State 4
Loss
State 5
Win
State 6
Win
State 7
?
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
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Documentopponent is in a winning state, and we lose.
But since state 7 is evidently a losing state,
then states 8, 9, and 11 are winning states.
State 1
Loss
State 8
Win
State 2
Win
State 9
Win
State 3
Win
State 10
?
State 4
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '09
 VLADIMIRLBOGINSKI
 Loss Win Win

Click to edit the document details