Sudoku Solving algorithms

In this section, we will try to solve the famous number maze problem called Sudoku. Sudoku is a 9 x 9 number grid, and the whole grid are also divided into 3 x 3 boxes There are some rules to solve the Sudoku.

We have to use digits 1 to 9 for solving this problem.

One digit cannot be repeated in one row, one column or in one 3 x 3 box.

Using the backtracking algorithm, we will try to solve the Sudoku problem. When some cell is filled with a digit, it checks whether it is valid or not. When it is not valid, it checks for other numbers. If all numbers are checked from 1-9, and no valid digit found to place, it backtracks to the previous option.

Input and Output

Input:
This will take a 9 x 9 matrix as Sudoku grid. Some values are placed in the grid. The blank spaces are denoted by 0.

Output:
The final matrix (Sudoku grid) filled with numbers. If the solution does not exist, it will return false.

3 1 6  | 5 7 8  | 4 9 2
5 2 9  | 1 3 4  | 7 6 8
4 8 7  | 6 2 9  | 5 3 1
------------------------
2 6 3  | 4 1 5  | 9 8 7
9 7 4  | 8 6 3  | 1 2 5
8 5 1  | 7 9 2  | 6 4 3
------------------------
1 3 8  | 9 4 7  | 2 5 6
6 9 2  | 3 5 1  | 8 7 4
7 4 5  | 2 8 6  | 3 1 9    

Algorithm

isPresentInCol(col, num)

Input: The column, and the targeted number.

Output − True when the number is present in the given column.

Begin
   for each row r in the grid, do
      if grid[r, col] = num, then
         return true
   done
   return false otherwise
End

isPresentInRow(row, num)

Input − The row, and the targeted number.

Output − True when the number is present in the given column.

Begin
   for each column c in the grid, do
      if grid[row, c] = num, then
         return true
   done
   return false otherwise
End

isPresentInBox(boxStartRow, boxStartCol, num)

Input − The starting row and column of a 3 x 3 box, and the targeted number.

Output − True when the number is present in the box.

Begin
   for each row r in boxStartRow to next 3 rows, do
      for each col r in boxStartCol to next 3 columns, do
         if grid[r, c] = num, then
            return true
      done
   done
   return false otherwise
End

findEmptyPlace(row, col)

Input: row and column in the grid.

Output − If the grid[row, col] is empty, then return true, otherwise false.

Begin
   for each row r in the grid, do
      for each column c in the grid, do
         if grid[r, c] = 0, then
            return true
      done
   done
   return false
End

isValidPlace(row, col, num)

Input: Row, a column of the grid, and number to check.

Output: True, when placing the number at position grid[row, col] is valid.

Begin
   if isPresentInRow(row, num) and isPresentInCol(col, num) and
   isPresntInBox(row – row mod 3, col – col mod 3, num) all are false, then
      return true
End

solveSudoku(Sudoku Grid)

Input: The unsolved grid of Sudoku.

Output: Grid after solve.

Begin
   if no place in the grid is empty, then
      return true
   for number 1 to 9, do
      if isValidPlace(row, col, number), then
         grid[row, col] := number
         if solveSudoku = true, then
            return true
         grid[row, col] := 0
   done
   return false
End

Example

#include 
#define N 9
using namespace std;

int grid[N][N] = {
   {3, 0, 6, 5, 0, 8, 4, 0, 0},
   {5, 2, 0, 0, 0, 0, 0, 0, 0},
   {0, 8, 7, 0, 0, 0, 0, 3, 1},
   {0, 0, 3, 0, 1, 0, 0, 8, 0},
   {9, 0, 0, 8, 6, 3, 0, 0, 5},
   {0, 5, 0, 0, 9, 0, 6, 0, 0},
   {1, 3, 0, 0, 0, 0, 2, 5, 0},
   {0, 0, 0, 0, 0, 0, 0, 7, 4},
   {0, 0, 5, 2, 0, 6, 3, 0, 0}
};

bool isPresentInCol(int col, int num) {    //check whether num is present in col or not
   for (int row = 0; row 

Output

3 1 6  | 5 7 8  | 4 9 2
5 2 9  | 1 3 4  | 7 6 8
4 8 7  | 6 2 9  | 5 3 1
------------------------
2 6 3  | 4 1 5  | 9 8 7
9 7 4  | 8 6 3  | 1 2 5
8 5 1  | 7 9 2  | 6 4 3
------------------------
1 3 8  | 9 4 7  | 2 5 6
6 9 2  | 3 5 1  | 8 7 4
7 4 5  | 2 8 6  | 3 1 9
Updated on: 2020-06-16T07:04:00+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements