A Sudoku puzzle
Have you ever solved Sudoku? It is a very popular game that requires you to fill empty cells of a 9x9 board with numbers from 1 to 9. However, each row, each column, and each 3x3 box must contain only unique numbers. An exemplary starting board and a solved one are shown as follows:
Figure 9.7 – An example of non-solved and solved Sudoku puzzles
Now, you will learn how to solve Sudoku not with the usage of a pencil and a piece of paper but with an algorithm! You can perform this task using the back-tracking approach, trying to assign numbers to empty cells if, of course, they meet the conditions regarding unique numbers in each row, column, and box. If an entered number does not result in solving the whole puzzle, you assign another number and perform the check again. Let’s take a look at the most important part of the code:
bool Solve() { (int row, int col) = GetEmpty(); ...