Problem Solving
Problem Solving
boxes.
2. Variables b and e for the number of boxes and the number of empty boxes, respectively, at an
3. Goal— determine the number of boxes given the number of empty boxes — to the
A common mistake is to try to count the number of medium boxes or the number of small boxes.
These are irrelevant, and a solution that introduces variables representing these quantities is over-
complicated. This is a key to effective problem solving: keep it simple!
THE TUMBLER PROBLEM
Several tumblers are placed on a table. Some tumblers are upside down, some are upside
up. It is required to turn all the tumblers upside up. However, the tumblers may not be
turned individually; an allowed move is to turn any two tumblers simultaneously.
From which initial states of the tumblers is it possible to turn all the tumblers
upside up?
Crossing River
Brute-force search means systematically trying all possibilities. Brute force does not
require any skill, but it does require a lot of careful and accurate work. Using brute
force is not something human beings are good at; lots of careful, accurate work is
something more suited to computers. But brute force is not even practical for
implementation on a computer. The amount of work involved explodes as the problem
size gets bigger, making it impractical for all but toy problems.
Nevertheless, it is useful to know what brute force entails, because it helps to
understand the nature of problem solving.
Nervous Couples
Three couples (president and bodyguard) wish to cross a river. They have one boat,
that can carry at most two people, making several trips across the river necessary.
The current is swift, the river is deep, and none of them can swim. Each president is
nervous about the other presidents' bodyguards, so no president is willing to be on a
bank or in the boat with another president's bodyguard, unless his or her own
bodyguard is also present.
The goal of subtraction games like Nim is to make your opponent lose, which is
usually done by changing the number of matchsticks that are left.
2. Tips for Winning Matchstick Games
a) Assumptions
Perfect Information: Both players know exactly what's going on in the game.
No Randomness: There is no chance in the game; the result is completely
determined by the moves of the players.
Ideal Play: Both players try to make it as difficult as possible for the other
player to win by playing ideally.
3. Labelling Positions
Labelling positions in matchstick games helps define which positions are winning
and losing. The key concepts are:
Winning Position: A position where the player to move can force a win no
matter how well the opponent plays.
Losing Position: A position where every possible move leaves the opponent in
a winning position.
4. Formulating Requirements for Winning Strategies
Work backwards from losing positions to identify which moves put the opponent in
these losing positions.
Apply the concept of "forced moves" to continuously push your opponent toward a
losing position.
5. Subtraction-Set Games
A Subtraction-Set Game is a type of game where, on each turn, a player can remove a fixed
number of matchsticks based on a pre-defined set of moves (e.g., remove 1, 2, or 3
matchsticks).
Rules: There are 21 matchsticks, and players can remove 1, 2, 3 or 4 matchsticks on their
“If both A and B play optimally (i.e- they don’t make any mistakes), then the player
starting first is guaranteed to win if the Nim-Sum at the beginning of the game is non-zero.
Otherwise, if the Nim-Sum evaluates to zero, then player A will lose.”
Bitwise XOR binary operation
Process: Nim-sum=3⊕4⊕3=2.
Grundy Numbers or Numbers determine how any Impartial Game (not only the Game
of Nim) can be solved once we have calculated the Grundy Numbers associated with
that game using Sprague-Grundy Theorem.
Mex?
‘Mex’ of a set of numbers is the smallest
non-negative number not present in the
set.
How to calculate Grundy Numbers?
The Grundy Number/ number is equal to 0 for a game that is lost immediately by the
first player and is equal to Mex of the numbers of all possible next positions for any
other game.
Example 1
The game starts with a pile of n matchsticks, and the player to move may take any
positive number of matchsticks. Calculate the Grundy Numbers for this game. The
last player to move wins. Which player wins the game?
Example 2
The game starts with a pile of n stones, and the player to move may take any positive
number of stones up to 3 only. The last player to move wins. Which player wins the
game? This game is 1 pile version of Nim.
Suppose there is a composite game (more than one sub-game) made up of N sub-games
and two players, A and B. Then Sprague-Grundy Theorem says that if both A and B play
optimally (i.e., they don’t make any mistakes), then the player starting first is guaranteed
to win if the XOR of the Grundy numbers of position in each sub-games at the beginning
of the game is non-zero. Otherwise, if the XOR evaluates to zero, then player A will lose,
no matter what.
How to apply Sprague Grundy
Theorem ?