
How to Play Queens¶
Figure 1:Example of a Queens minigame board. Source: LinkedIn Queens
- Objective
- To place a crown in each row, column, and colored region on the board.
- Rules
- There can only be one crown in each row, column and colored region;
- There cannot be adjacent crowns, not even along adjacent diagonals.
Problem Modeling¶
Before solving Queens, it is necessary to translate the game elements into the following components of the LOP:
Ranges
Sets
Decision Variables
Objective Function
Constraints
Ranges¶
Defining the ranges is a fundamental step for the proper definition of the other components of the LOP. For the case of Queens, three ranges will be considered:
- Range of rows, where is the total number of rows on the board (in this case, = 7)
- Range of columns, where is the total number of columns on the board (also = 7 for this case)
- Range of colored regions in game, where is the total number of regions on the board ( = 7)
Sets¶
By defining the ranges and , let’s define three sets for solving the Queens game:
- Set of all squares on the board, which is nothing more than the Cartesian product of the ranges and
- Set of colored regions and says to which region a square belongs.
- Set of all pairs of diagonally adjacent squares. This set will be useful so to make defining the constraints easier later on.
Decision variables¶
The decision variables will be binary variables , which will assume only two values:
- , if the crown is located in row and column ;
- , otherwise.
Objective function¶
The interesting thing about the Queens game is that, since we don"t have an objective function to maximize or minimize, we are not actually dealing with an optimization problem, but rather a feasibility problem, that is, our objective is only to find a feasible solution that meets all the game’s constraints. Because of this, we can define the objective function as minimizing (or maximizing, it doesn"t matter) an arbitrary constant. Thus, the objective function will not depend on the decision variables, so that the model doesn"t seek to optimize it and only worries about meeting the constraints.
Constraints¶
With the previous elements well defined, we can finally translate Queens’ rules into constraints for our BLOP model. Let’s go step by step:
- Binarity Constraints
- The first constraint we need to define right away is that all decision variables in our problem are binary variables, meaning they only accept 0 and 1 as valid values.
- Single-Crown-Per-Row Constraints
- Since each row on the board must have only one crown, the sum of all belonging to a row must equal 1. As there are 7 rows in total, there will be 7 such constraints, one for each row.
- Single-Crown-Per-Column Constraints
- The same logic applies to the columns. Since there are 7 columns on the board, there will be 7 more constraints, one for each column.
- Single-Crown-Per-Region Constraints
- The same logic also applies to the colored regions. For each region of the board, the sum of the belonging to a region must be equal to 1. Since there are 7 regions, there will be 7 more constraints.
- Diagonally-Adjacent-squares Constraints
- Finally, we must not forget the rule that there cannot be adjacent crowns, not even along the diagonals. The cases of vertical and horizontal proximity do not need to be addressed, since the row and column constraints already cover this. Therefore, we only need to worry about imposing constraints for the two diagonal directions in each pair of shared-vertex squares. With the set well defined, this constraint is easily defined as it follows.
Abstract Model¶
With all components defined, it is possible to model the game of Queens using the following abstract model.
Concrete Model¶
In this article, let’s apply LO techniques to solve the Queens No. 307, which will be an instance of the abstract model presented earlier, that is, its concrete model.

Figure 2:Queens No. 307, March 3rd, 2025. Source: LinkedIn Queens
The concrete model for the game above will be the following:
S.t.:
- Single-Crown-Per-Row Constraints
- (Row 1)
- (Row 2)
- (Row 3)
- (Row 4)
- (Row 5)
- (Row 6)
- (Row 7)
- Single-Crown-Per-Column Constraints
- (Column 1)
- (Column 2)
- (Column 3)
- (Column 4)
- (Column 5)
- (Column 6)
- (Column 7)
- Single-Crown-Per-Region Constraints
- (Purple Region)
- (Orange Region)
- (Blue Region)
- (Green Region)
- (Gray Region)
- (Red Region)
- (Yellow Region)
- Principal Diagonals Constraints
- Secondary Diagonals Constraints
- Binarity Constraints
Solving Queens¶
In order to solve the presented game, the linkedin-games library counts on Queens class, which implements the Queens game and its constraints, as well as methods to solve it and visualize the game’s solution.
So, first of all, let’s import the Queens class and create an instance of it, passing its board dimensions as a (rows, columns) tuple and its colored regions as a dictionary of color: {squares} items, as shown bellow:
from linkedin_games import Queens
regions = {
"#BBA3E1": { # Purple
(1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (2,6),
(2,7), (3,6), (3,7), (4,6), (4,7), (5,7), (6,7), (7,7)
},
"#FFC794": { # Orange
(2,1), (2,2), (2,3), (2,4), (3,1), (4,1), (4,2),
(5,1), (5,2), (6,1), (6,2), (6,4), (6,5), (6,6),
(7,1), (7,2), (7,3), (7,4), (7,5), (7,6)
},
"#94BEFF": {(2,5), (3,5)}, # Blue
"#B3DF9E": {(3,2), (3,3)}, # Green
"#E0E0E0": {(3,4), (4,3), (4,4), (4,5), (5,4)}, # Gray
"#FF7B61": {(5,3), (6,3)}, # Red
"#E6F388": {(5,5), (5,6)} # Yellow
}
queens = Queens(7, regions)The Queens class features the model attribute, which implements a QueensModel class that, in its turn, implements the Linear Optimization logic of the Queens game.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51import pyomo.environ as pyo class QueensModel(pyo.ConcreteModel): """The Linear Optimization model for the Queens game.""" def __init__(self, grid_dims: tuple[int, int], regions: dict[str, set[tuple[int, int]]]) -> None: """ Args: grid_dims: Grid dimensionas as a `(rows, columns)` tuple. regions: All colored regions on grid as a dictionary of `color: {(row, column), ...}` items. """ super().__init__() # BOARD DIMENSIONS m, n = grid_dims self.m = pyo.Param(initialize=m, domain=pyo.PositiveIntegers) self.n = pyo.Param(initialize=n, domain=pyo.PositiveIntegers) # RANGE SETS I = self.I = pyo.RangeSet(n) # Rows J = self.J = pyo.RangeSet(m) # Columns K = self.K = pyo.Set(initialize=regions.keys()) # Colored Regions # COMPOSITE SETS S = self.S = pyo.Set(initialize=lambda model: [(i, j) for i in I for j in J]) # Grid Squares R = self.R = pyo.Set(K, initialize=regions, dimen=2, domain=S) # Region Squares D = self.D = pyo.Set(initialize=lambda model: # Diagonals [((i, j), (i + 1, j + 1)) for (i, j) in S if (i + 1, j + 1) in S] + [((i, j), (i + 1, j - 1)) for (i, j) in S if (i + 1, j - 1) in S] ) # OBJECTIVE FUNCTION self.obj = pyo.Objective(expr=0) # feasibility problem # DECISION VARIABLES x = self.x = pyo.Var(S, domain=pyo.Binary, initialize=0) # CONSTRAINTS self.single_crown_per_row_constraints = pyo.Constraint( I, rule=lambda model, i: pyo.quicksum(x[i, j] for j in J) == 1 ) self.single_crown_per_column_constraints = pyo.Constraint( J, rule=lambda model, j: pyo.quicksum(x[i, j] for i in I) == 1 ) self.single_crown_per_region_constraints = pyo.Constraint( K, rule=lambda model, k: pyo.quicksum(x[i, j] for (i, j) in R[k]) == 1 ) self.adjacent_squares_by_vertex_constraints = pyo.Constraint( D, rule=lambda model, i, j, r, s: x[i, j] + x[r, s] <= 1 )
Program 1:Creating the model with Pyomo components.
With model constructed, the public method solve() calls internally the restricted _set_solution() one to save the solution to the private attribute __crowns, which indicates the crowned squares in the game and can be accessed by the public attribute crowns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14""" The crowned squares of Queens game. Returns: Locations of all crowns as a list of squares as `(row, column)` or `None` if the game is not solved yet. """ if not self.is_solved: return None return sorted((i+1, j+1) for (i, j) in self.__crowns.nodes()) def _set_solution(self, verbose:bool = False) -> None:
Program 2:Saving the game’s solution to __crowns attribute.
1 2 3 4 5 6 7 8) raise ValueError(msg) @property def crowns(self) -> list[tuple[int, int]] | None:
Program 3:Implementation of crowns attribute.
With the solution obtained, the method show() displays the results as a Matplotlib plot showing the board with the crowned squares marked on it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38x = self.model.x S = self.model.S nx.set_node_attributes( self.grid, name="value", values={(i-1, j-1): round(pyo.value(x[i, j])) for (i, j) in S} ) crowns = [square for square, value in nx.get_node_attributes(self.grid, "value").items() if value == 1] self.__crowns = self.grid.subgraph(crowns) if verbose: print("These are the squares that contain a crown:") pprint(self.crowns) def show(self) -> None: """Show the Queens' grid.""" width = height = self.size * 0.5 plt.figure(figsize=(width, height)) nx.draw( self.grid, pos={(i, j): (j, -i) for i, j in self.grid.nodes()}, with_labels=True, arrows=False, labels= dict.fromkeys(self.__crowns.nodes(), "O") if self.__crowns is not None else dict.fromkeys(self.grid.nodes(), ""), node_size=1100, node_color=list(nx.get_node_attributes(self.grid, "color").values()), node_shape="s", # Squared-shape nodes width=0, edgecolors="black", linewidths=.5 ) plt.show()
Program 4:Implementation of show() function.
So to solve the game and display its results, just call the public methods solve() and show().
queens.solve()
queens.show()
The output board matches the solution of the Queens No. 307, as expected.

Figure 3:Solution of Queens No. 307, March 3rd, 2025. Source: LinkedIn Queens