Skip to content

Instantly share code, notes, and snippets.

@rijesha
Created January 8, 2018 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rijesha/ede4ef3782da3135c55451855e6c6c76 to your computer and use it in GitHub Desktop.
Save rijesha/ede4ef3782da3135c55451855e6c6c76 to your computer and use it in GitHub Desktop.
C code for solving sudoku recursively
#include <stdlib.h>
#include <stdio.h>
int solve(int i, int j);
int recursiverowcheck(int i, int j, int n);
int recursivecollumncheck(int i, int j, int n);
int recursiveboxcheck(int i, int j, int n);
void printpuzzle(void);
/*
int sudoku[9][9] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}*/
int sudoku[9][9] = {
{ 2, 0, 0, 5, 0, 3, 0, 0, 8 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 4, 0, 0, 0, 6, 7, 0 },
{ 0, 3, 0, 1, 0, 7, 0, 9, 0 },
{ 0, 5, 0, 0, 9, 0, 0, 6, 0 },
{ 0, 8, 0, 4, 0, 6, 0, 1, 0 },
{ 0, 9, 1, 0, 0, 0, 2, 8, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 5, 0, 0, 7, 0, 2, 0, 0, 6 }
};
void main()
{
/*int sudoku[9][9] = {
{ 2, 0, 0, 5, 0, 3, 0, 0, 8 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 4, 0, 0, 0, 6, 7, 0 },
{ 0, 3, 0, 1, 0, 7, 0, 9, 0 },
{ 0, 5, 0, 0, 9, 0, 0, 6, 0 },
{ 0, 8, 0, 4, 0, 6, 0, 1, 0 },
{ 0, 9, 1, 0, 0, 0, 2, 8, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 5, 0, 0, 7, 0, 2, 0, 0, 6 }
};
int randomarray[9] = { 3, 5, 2, 6, 7, 8, 1, 9, 4 };
int arr[9][9] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};*/
int i = 0, j = 0;
int q=solve(i, j);
printpuzzle();
system("pause");
}
int solve(int i, int j)
{
int permanent = 0;
if (sudoku[i][j] != 0)
{
permanent = 1;
}
if (permanent == 1)
{
if (i == 8 && j == 8)
{
return 1;
}
else
{
if (j == 8)
{
if (solve(i + 1, 0) == 1)
{
return 1;
}
return 0;
}
else
{
if (solve(i, j + 1) == 1)
{
return 1;
}
return 0;
}
}
}
for (int n = 1; n < 10; n++)
{
if ((recursiveboxcheck(i, j, n) == 0 && recursivecollumncheck(i, j, n) == 0 && recursiverowcheck(i, j, n) == 0))
{
if (permanent==0)
sudoku[i][ j] = n;
if (i==8 && j==8)
{
return 1;
}
else
{
if (j == 8)
{
if (solve(i+1, 0) == 1)
{
return 1;
}
}
else
{
if (solve(i, j+1) == 1)
{
return 1;
}
}
}
}
}
if (permanent == 0)
sudoku[i][j] = 0;
return 0;
}
int recursiverowcheck(int i, int j, int n)
{
for (int l = 0; l < 9; l++)
{
if (sudoku[i][l] == n)
{
return 1;
}
}
return 0;
}
int recursivecollumncheck(int i, int j, int n)
{
for (int l = 0; l < 9; l++)
{
if (sudoku[l][j] == n)
{
return 1;
}
}
return 0;
}
int recursiveboxcheck(int i, int j, int n)
{
for (int boxrow = (i / 3) * 3, rmax = boxrow + 3; boxrow < rmax; boxrow++)
{
for (int boxcollumn = (j / 3) * 3, cmax = boxcollumn + 3; boxcollumn < cmax; boxcollumn++)
{
if (sudoku[boxrow][boxcollumn] == n)
{
return 1;
}
}
}
return 0;
}
void printpuzzle(void)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
printf("%d", sudoku[i][j]);
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment