Technique
Combinatorial Game Theory (minimax, Nim, Sprague-Grundy)
Classify positions as winning/losing by DP over states, and combine independent games with Grundy numbers XORed together.
0/10 solved
Recognise it
The signal
two players alternate optimally, who wins
1Brute force
Full game-tree search.
2The observation
Exponential game tree: positions repeat and independent games combine by XOR of Grundy values.
3Why it works
A position is losing iff every move leads to a winning position; grundy = mex of successors.
Reach for it when
- +piles / independent subgames
- +take from ends of an array
Not when
- ×games with scores rather than win/lose (minimax interval DP)
- ×non-impartial games (Sprague-Grundy does not apply)
Practice
- Stone GameMedium
- Can I WinMedium
- Stick GameCSESMedium
- Nim Game ICSESMedium
- Stair GameCSESMedium
- Mex Grid QueriesCSESMedium
- Removal GameCSESMedium
- Number GameCodeforces1400
- Another GameCSESHard
- Grundy's GameCSESHard
Pitfalls
Misconceptions
- !assuming the first player always wins in symmetric-looking games
Looks like it, isn't
- Cat and Mouse needs Games on Graphs (retrograde analysis)
Why
Positions repeat and draws exist, so memo DFS has no valid order. The editorial percolates results back from resolved states. source ↗ - Chalkboard XOR Game needs Invariants & Parity Arguments
Why
The editorial's argument: Alice wins iff the XOR is 0 or n is even. No game search. source ↗
Go deepervariants and techniques · 2 combinations · resources
Combinations
Games on Graphs (retrograde analysis)+Topological Sort+BFS/Dijkstra on an Augmented State Graph
chase game on a graph or grid with possible draws
- Games on Graphs (retrograde analysis):
- labels propagate back from terminal states
- Topological Sort:
- Kahn-style degree counters decide LOSE once every move is known to lose
- BFS/Dijkstra on an Augmented State Graph:
- states are (mouse, cat, turn)
Combinatorial Game Theory (minimax, Nim, Sprague-Grundy)+Bitmask DP
two players pick from a small pool (n <= 20) without replacement
- Combinatorial Game Theory (minimax, Nim, Sprague-Grundy):
- a state is winning if some move leads to a losing state
- Bitmask DP:
- the set of used numbers is the state