Amplitud) and DFS (Profundidad) — both using the same cell-expansion logic.
The initial board is hardcoded inside
main(). To solve a different puzzle you must edit the source file and recompile.Compile and run
Initial board
The puzzle hardcoded inmain() is:
·):
matrizAux before compiling. Use ' ' (a space character) for empty cells.
Search algorithms
Both solvers share the same expansion strategy: find the first empty cell in row-major order, try digits'1' through '9', and for each digit that passes all three validity checks, create a new sudoku state with that digit placed and add it to the list. Only the first empty cell is expanded per iteration.
- Amplitud (BFS)
- Profundidad (DFS)
Amplitud() uses a std::list as a FIFO queue. New states are appended to the back with push_back; the current state is taken from the front with pop_front and lista.front().BFS explores boards with the same number of filled cells before going deeper, so it tends to use significantly more memory than DFS on Sudoku inputs.
Validation methods
The threeverify* methods are called before placing any digit to ensure the placement does not break Sudoku rules. Each returns true if numAux is already present in the given row, column, or region.
false (meaning the digit does not yet appear in the row, column, or region).
Region numbering
Regions are numbered 1–9 in reading order (left to right, top to bottom):| Region | Rows | Columns |
|---|---|---|
| 1 | 0–2 | 0–2 |
| 2 | 0–2 | 3–5 |
| 3 | 0–2 | 6–8 |
| 4 | 3–5 | 0–2 |
| 5 | 3–5 | 3–5 |
| 6 | 3–5 | 6–8 |
| 7 | 6–8 | 0–2 |
| 8 | 6–8 | 3–5 |
| 9 | 6–8 | 6–8 |
(i, j) with:
verificarResolucion()
Checks that every row, column, and region contains each of the digits '1' through '9' exactly once. Returns true only when all 27 checks pass:
Class reference
class sudoku
class sudoku
Represents a single 9×9 board state.Private members
Public methods
| Member | Type | Description |
|---|---|---|
matriz | char[9][9] | The 81-cell board. Empty cells are stored as ' '. |
F_Inicio | int | Row offset used internally by verificarRegion(). |
C_Inicio | int | Column offset used internally by verificarRegion(). |
| Method | Description |
|---|---|
sudoku() | Default constructor. Fills all 81 cells with ' '. |
sudoku(char mtzUsuario[9][9]) | Constructs from an existing 9×9 char array. |
void imprimir() | Prints the board with row separators and column pipes. |
bool verificarFila(char numAux, int noFila) | Returns true if numAux is already in row noFila. |
bool verificarColumna(char numAux, int noColumna) | Returns true if numAux is already in column noColumna. |
bool verificarRegion(char numAux, int region) | Returns true if numAux is already in the 3×3 region numbered 1–9. |
bool verificarResolucion() | Returns true if every row, column, and region contains '1'–'9'. |
class problemaSudoku
class problemaSudoku
Owns the initial board and runs the search.Private members
Public methods
| Member | Type | Description |
|---|---|---|
inicial | sudoku | The starting board state passed to the constructor. |
| Method | Description |
|---|---|
problemaSudoku(sudoku) | Constructor. Stores the initial state and prints the board. |
void Amplitud() | Runs BFS. Prints the solved board and iteration count when a solution is found. |
void Profundidad() | Runs DFS. Prints the solved board and iteration count when a solution is found. |