Skip to content

Instantly share code, notes, and snippets.

@kapil1024
Last active March 21, 2021 06:42
Show Gist options
  • Save kapil1024/7507de190621a2a4ec155a99efce8966 to your computer and use it in GitHub Desktop.
Save kapil1024/7507de190621a2a4ec155a99efce8966 to your computer and use it in GitHub Desktop.
C code to solve any Sudoku puzzle
#include<stdio.h>
// A sample puzzle
int sudoku[9][9] = { {0,0,0,4,0,0,0,9,0},
{0,3,0,0,5,0,0,1,8},
{0,0,0,0,0,0,6,0,7},
{4,2,0,1,0,3,0,0,6},
{0,9,0,0,0,0,0,2,0},
{7,0,0,2,0,8,0,4,1},
{6,0,4,0,0,0,0,0,0},
{9,8,0,0,2,0,0,6,0},
{0,5,0,0,0,9,0,0,0} };
// Print Sudoku puzzle on the screen
void printSudoku (void) {
int x;
int y;
for (x = 0; x < 9; x++) {
for (y = 0; y < 9; y++) {
if (y%3 == 0) printf ("+");
if (x%3 == 0) {
printf("+===");
} else {
printf("+---");
}
}
printf ("++\n");
for (y = 0; y < 9; y++) {
if (y%3 == 0) printf ("|");
printf ("| %d ", sudoku[x][y]);
}
printf ("||\n");
}
for (y = 0; y < 9; y++) {
if (y%3 == 0) printf ("+");
printf("+===");
}
printf ("++\n");
}
// Check if it is possible to put a number "n" at given cell (x,y)
int isPossible (int x, int y, int n) {
int i;
int j;
int bx;
int by;
// Empty cell?
if (sudoku[x][y] != 0) {
return 0;
}
// Check row
for (i = 0; i < 9; i++) {
if (sudoku[x][i] == n) {
return 0;
}
}
// Check column
for (j = 0; j < 9; j++) {
if (sudoku[j][y] == n) {
return 0;
}
}
// Check in corresponding 3x3 box
bx = (x/3) * 3;
by = (y/3) * 3;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (sudoku[bx + i][by + j] == n) {
return 0;
}
}
}
// Not found anywhere?
return 1;
}
// Try to solve given sodoku puzzle and find all possible solutions (if any).
// It is a recursive function which returns when
// 1. There are no empty cells i.e. solved.
// 2. There is a conflict (i.e. cannot put any number) in a cell.
void solve (void) {
int x;
int y;
int n;
for (x = 0; x < 9; x++) {
for (y = 0; y < 9; y++) {
if (sudoku[x][y] == 0) {
for (n = 1; n < 10; n++) {
if (isPossible (x, y, n) == 1) {
// Assume n is correct value and try to solve the puzzle
sudoku[x][y] = n;
solve ();
// We might be here because of some conflict during previous call
// Clear the value to try next one
sudoku[x][y] = 0;
}
}
// No match. Conflict!!
return;
}
}
}
// We are here because one of the recursive call has solved the puzzle.
// Print the solution here.
printSudoku ();
// Below return will be considered as a conflict by caller
// and it will proceed in search of anther solutions (if any)
// so we will lose this solution. If required, save the solution in some other buffer.
return;
}
// Get an unsolved Sudoku puzzle from user.
void getSudoku (void) {
int x;
int y;
int n;
printf ("Enter Sudoku puzzle cell by cell:\n");
for (x = 0; x < 9; x++) {
for (y = 0; y < 9; y++) {
printf ("Enter element (%d,%d):", x, y);
scanf ("%1d", &n);
sudoku[x][y] = n;
}
}
}
// Main function
int main (int argc, char** argv) {
getSudoku (); // Remove this line if you just want to solve sample puzzle.
printf ("Unsolved Sudoku puzzle:\n");
printSudoku ();
printf ("Solution:\n");
solve ();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment