Series: Modeling logic puzzles
Phần: 2 / 91. Introduction
Star Battle is a logic puzzle defined as follows: given an grid divided into regions, players must fill the grid with stars that satisfy the following requirements:
- Each row, column, and region contains a specified number of stars.
- No two stars can be adjacent (in any direction).
Below is an example of a 10x10 Star Battle puzzle, with 2 stars in each row, column, and region

2. Modeling the puzzle
Some symbols used in this problem
- is the number of rows in the grid
- is the number of columns in the grid
- is the number of stars to be placed in each row, column, and region
- is the set of regions
- is the set of cells belonging to region
- is the set of neighboring cells of cell (including diagonals)
- is the number of neighboring cells of cell (including diagonals)
2.1. Decision variables
For each cell in the grid, we define a variable such that
2.2. Constraints
- Each row contains exactly stars
- Each column contains exactly stars
- Each region contains exactly stars
-
No two stars can be adjacent (in any direction).
This condition can be rephrased as follows: if cell is filled with a star, then all cells adjacent to cell cannot be filled with a star.
Linearizing the above condition using geometric method, we obtain the following constraint, which is the one used in the model.
2.3. Objective function
This problem does not have an objective function, as we are not aiming to minimize or maximize any value. Our goal is simply to find a feasible solution. Technically, when programming, we can set the objective function to a constant (I often choose 0)
3. Conclusion
For more Star Battle puzzles and variations, please refer to Krazydad. The Python code for Star Battle modeling is available at Tung-hehe.
Happy modeling!


