Coders Packet

Solve Sudoku in C++

By Anirudh C V

Solve an Incomplete Sudoku configuration in terms of a 9 x 9 Square Matrix with help of Backtracking in C++

We solve an Incomplete Sudoku configuration in terms of a 9 x 9 Square Matrix with the help of Backtracking, for simplicity, we assume that there will be only one unique solution. The function SolveSudoku() takes a grid as its argument and returns true if a solution is possible and false if it is not. The function FindUnassignedLocation() finds the location of any unassigned or 0 in the matrix.

INPUT :

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

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

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Anirudh C V (AnirudhCV)

Download packets of source code on Coders Packet