This article marks the beginning of a series dedicated to exploring a special class of problems: Constraint Satisfaction Problems (CSPs).
In general, a CSP consists of assigning values to a set of variables, chosen from a given domain, in such a way that all imposed constraints are satisfied. This is a highly generic theory, which allows us to apply it to countless challenges in computer science.
To demonstrate its versatility, throughout this series we will see how CSPs can be used to solve famous puzzles, such as Minesweeper, Sudoku, and the 8 Queens Problem, as well as practical applications like scheduling, resource allocation, and even formal proofs of algorithms.
However, before diving into more rigorous definitions, we’ll start with an intuitive approach: in the first articles, we’ll present some problems and develop initial modelings, without worrying too much about formalization. This will help us build the reasoning and intuition behind CSPs.
Minesweeper
We’ll begin with a classic problem—especially if you’ve ever used Windows and lost your internet connection—the Minesweeper game.
Minesweeper is a game where the player needs to reveal all the cells on a board without clicking on a mine. Each cell can be revealed by clicking on it, and if it contains a bomb, the game ends. If, on the other hand, the chosen cell is safe, one of two things can happen:
- A number appears, indicating the number of mines adjacent to the chosen cell.
- No number appears. In this case, there are no bombs adjacent to the cell, and the game will automatically reveal all neighboring cells until it reaches cells that do have adjacent bombs.
In practice, the player doesn’t need to guess every safe cell exactly, since revealing one safe region may uncover a large part of the board automatically. The interesting part is that, given the numbers on the revealed cells, we can combine this information to infer where new bombs may be located—or where cells are safe—even though there will always be an element of luck involved.
A Practical Example
Consider a Minesweeper puzzle with 4 rows, 4 columns, and 2 bombs. Initially, all its cells are hidden, so we have something like this:

The player may choose any cell to start the game. Suppose we pick the cell (row=1, col=1), and the game continues to the following state:

Those with numbers represent the number of adjacent bombs.
In this example, the first two rows had no adjacent bombs, so they are revealed with zeros. The third row is also revealed, but its cells have bombs nearby.
Now, let’s create a series of logical arguments that allow us to infer whether a cell is safe or contains a bomb, guiding our next moves.
To do this, we’ll enumerate all the statements we already know to be true.
We use (a,b) to represent the cell in row a and column b, indexed from one.
- If there is one bomb adjacent to (3,1), then there must be one bomb between (4,1) and (4,2):
If there are two bombs adjacent to (3,2), then there must be two bombs among (4,1), (4,2), and (4,3):
If there is one bomb adjacent to (3,3), then there must be one bomb among (4,2), (4,3), and (4,4):
If there is one bomb adjacent to (3,4), then there must be one bomb among (4,3) and (4,4):
Now, let’s analyze each statement:
- (2) tells us there must be two bombs among (4,1), (4,2), and (4,3). But from (1), we know there’s one bomb between (4,1) and (4,2). Therefore, (4,3) must be a bomb.
- Next, (3) says there is one bomb among (4,2), (4,3), and (4,4). Since we already know (4,3) is a bomb, (4,2) and (4,4) must be safe.
- Finally, we know there are exactly two bombs on the board, and since (4,3) is one of them, only one bomb remains. The only possibility is (4,1).
With this reasoning, we could reveal the safe cells and, once no uncertainty remains, win the game!
An Initial Modeling
Our goal now is to try to create a formal treatment so that a computer program could replicate the kind of reasoning we just made. For this, let’s set some quick definitions:
Note: At this point, we’re not aiming for a rigorous formalization—we’ll get there later.
Given a Minesweeper board, we define:
Definition 1.1: Check if a cell is a bomb
For a cell on a board , we say:
For instance, , while .
Definition 1.2: Neighborhood of a cell
Given a cell on a board , its neighborhood, denoted by , is the set of all cells adjacent to . Here, adjacency includes diagonals.
For example, in a 3x3 board, the neighborhood of (2,2) is:
We can then count the number of adjacent bombs:
Definition 1.3: Adjacent Bombs
For a cell on a board , we say if is the number of bombs in the neighborhood of . Formally,
If we represent the revealed board from the example in matrix form:
Now, define . Based on the state of the game, we can derive the following system of equations:
Equation (v) is the global constraint: it tells us the total number of bombs is 2.
By solving this system, we reach a unique solution:
In general, for a board with rows, columns, and bombs, there are possible solutions. For instance, with a 4x4 board and 4 bombs, there are solutions!
Revealing cells effectively means adding constraints to reduce the solution space. When we solve the system of equations above, we are essentially treating each cell as a variable that can only take values 0 or 1, while each equation imposes a constraint on these variables. This process narrows down the possibilities until, in some cases, only a single consistent solution remains.
This is precisely why Minesweeper can be approached as a CSP: winning the game is nothing more than finding an assignment of values to the board’s variables that satisfies all imposed constraints simultaneously.
Conclusion
Minesweeper is a perfect example of how seemingly simple problems can be reformulated as Constraint Satisfaction Problems (CSPs). By translating the board into variables, domains, and constraints, we open the door to applying general techniques that go far beyond guessing or luck.
This perspective shift—from a casual game to a formal problem of optimization and search—allows us to draw connections with other classic challenges in computer science.
In this first article, we’ve only scratched the surface, structuring the problem and taking the first steps in modeling. In the upcoming posts, we’ll explore other examples before diving into how these problems can be solved in practice.
In the next article, we’ll explore another classic challenge—the Map Coloring Problem—and see how the same logic applies even more clearly.
Top comments (0)