Series: Modeling logic puzzles
Phần: 3 / 91. Introduction
Galaxies is a logic puzzle described as follows: given a grid of squares with dots representing stars, player needs to divide the grid into regions called galaxies, satisfying the following conditions:
- The grid is completely covered by galaxies.
- Galaxies do not overlap.
- Each galaxy must and only contains one star in the center as its core.
- Each galaxy must be centrally symmetric with respect to the star in the center.
Reference: Wikipedia
Below is an example of a 10×18 Galaxies puzzle and its solution

2. Modeling the puzzle
- is the number of rows in the grid
- is the number of columns in the grid
- is the number of galaxies (equal to the number of stars in the grid)
- is the set of cells with coordinates containing the centers of galaxy
- is the set of cells that can possibly belong to galaxy
- is the set of all paths from to the center of galaxy
For example, in the figure below, with being the galaxy containing the black star, contains all cells in blue border rectangle, contains all cells in red border region and green line is a path from orange cell to the center of galaxy .

- is the set of cells that belong to path
- is the number of cells in path
2.1. Decision variables
For each cell in the grid and a galaxy (), we define a variable such that
2.2. Constraints
- The grid is completely covered by galaxies, and the galaxies do not overlap. These two constraints are equivalent to the following constraint: each cell in the grid must belong to, and only belong to, one galaxy.
- Each galaxy contains its core
- Each galaxy must be centrally symmetric with respect to the star in the center
Where is the cell symmetric to cell with respect to the center of galaxy
Note: The above conditions are sufficient to solve small Galaxies puzzles (7×7, 8×8, ...). However, when solving larger puzzles (10×10 or larger) we encounter a problem where the galaxy can be divided into many separate small pieces.
-
The cells within a galaxy must be connected. This condition is equivalent to the following: for any two cells belonging to the same galaxy, there must exist at least one path between them such that all cells on this path also belong to that galaxy.
To linearize this condition, we introduce additional intermediate variables for each galaxy , cell and each path satisfying
In other words, the variable equals 1 if cell is connected to the center of galaxy through path . Using geometric method we can represent the variables through the variables by the following constraint:
Finally, the original condition can be linearized as follows:
-
Additionally, to reduce the search space of the problem, we can add the condition that cells not in will not belong to galaxy
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
The highlight of this puzzle is the last two conditions, each of which has a different effect but shares a common point: if not carefully considered, we can easily overlook them. To avoid this situation, we need to think carefully about the problem and try solving different many cases to detect any issues (if any)
For more Galaxies puzzles and variations, please refer to Krazydad. The Python code for Galaxies modeling is available at Tung-hehe.
Happy modeling!


