Lights Out - finding worst initial state
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Lights Out is a classic puzzle game that challenges players to turn all lights off on a grid, typically 5x5, by toggling them through specific rules. Each move toggles a light and its immediate neighbors (up, down, left, right). The complexity of Lights Out puzzles arises from finding the optimal strategy, especially in determining the initial state that presents the maximum challenge. This article explores the intricacies involved in identifying the worst-case initial state and understanding its technical underpinnings.
Understanding Lights Out Mechanics
In Lights Out, the grid is represented as a matrix where each cell can be either lit (1) or unlit (0). The goal is to transition all cells to an unlit state by toggling them according to the aforementioned rule.
Mathematical Representation
The grid can be mathematically represented as a binary matrix, and each move can be seen as a vector in a finite field of size two . The solution to the puzzle is determined through linear algebraic techniques by expressing the problem as a system of linear equations:
• Let be the state of the grid cells (either 0 or 1). • Define a toggle matrix , where if a toggle at cell affects cell , otherwise . • The solution involves finding a vector such that , where is the desired end state (all zeros indicating lights out).
Finding the Worst Initial State
The quest for the worst-case initial state involves identifying a state that maximally challenges the solver, either by requiring the greatest number of moves or demonstrating the highest complexity in terms of toggles.
Hilbert Space Analysis
Lights Out can be translated into the language of Hilbert space by considering the linear independence of states. The initial configuration of lights that maximizes the rank of toggle sequences can be misleadingly complex, creating what is deemed as the "worst-case" scenario.
Examples and Strategies
A well-known worst-case configuration involves starting with a checkerboard pattern in a 5x5 grid. This layout requires maximal consideration of each light toggled and often illustrates the non-intuitive nature of the puzzle mechanics.
Example Configuration
Consider a 5x5 grid:
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
• Exhaustive Search: Testing all possible initial states and recording those that require the maximum number of moves. • Genetic Algorithms: Utilizing heuristic methods to evolve configurations over generations that approach the boundary of highest difficulty.
Related reading
- Limit number of cores used in Keras
- Limit number of threads in numpy
- Limit Tensorflow CPU and Memory usage
- Limit the number of pods per node
- Limiting the depth of Boolean expressions
- Linear Search Algorithm Optimization
- Linear time algorithm for Minimum number of jumps required to reach end
- Linear Time Voting Algorithm. I don't get it

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.