Given N piles containing White(W) and Black(B) boxes only, two players A and B play a game.
Player A may remove any number of boxes from the top of the pile having the topmost box White and Player B may remove any number of boxes from the top of the pile having the topmost box Black. If there is a pile with top as W, then player A has remove one or more boxes. Similarly if there is a pile with top as B, then player B has to remove one or more boxes. They play alternating, with the player A first to move. Both players plays optimally.
The task is to find the winner of the game (who cannot make the last move). Player last to move loses the game.
Input: N = 2
Player A can remove all boxes from pile 1. Now player B has to make a choice from pile 2. Whatever choice player B makes, he/she has to make the last move.
Input: N = 3
WWWW, WBWB, WBBW
Approach: The game theory problems, where the player who makes the last move lose the game, are called Misere Nim’s Game.
- If there are consecutive occurrences of the same box in any pile, we can simply replace them with one copy of the same box, since it is optimal to remove all occurrences of the box present at the top. So, we can compress all piles to get the pile of form BWBWBW or WBWBWB.
- Now, suppose there’s a pile, which have topmost box differing from its bottom-most box, we can prove that answer remains same even without this pile, because if one player makes a move on this pile, other player makes the next move by removing box from the same pile, resulting in exactly same position for the first player.
- If a pile has both top and bottom-most box as same, It requires one or the player to make one extra move. So, just count the number of extra move each player has to make. The player which runs of out extra moves first wins the game.
Below is the implementation of the above approach.
Time Complexity: O(N)
- Optimal Strategy for a Game | DP-31
- Combinatorial Game Theory | Set 1 (Introduction)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Combinatorial Game Theory | Set 3 (Grundy Numbers/Nimbers and Mex)
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)
- Minimax Algorithm in Game Theory | Set 1 (Introduction)
- Minimax Algorithm in Game Theory | Set 2 (Introduction to Evaluation Function)
- Implementation of Tic-Tac-Toe game
- Minimax Algorithm in Game Theory | Set 4 (Alpha-Beta Pruning)
- Minimax Algorithm in Game Theory | Set 5 (Zobrist Hashing)
- The prisoner's dilemma in Game theory
- A modified game of Nim
- Game of N stones where each player can remove 1, 3 or 4
- Game of replacing array elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.