Last active
July 9, 2019 12:43
-
-
Save ariG23498/50fb66b2b3efc14d24ae6e2f43b325c6 to your computer and use it in GitHub Desktop.
sudoku_solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#define N 9 | |
#define unassigned 0 | |
//int a[N][N]; | |
//int numbers=0; | |
int i,j,k; //the loop arrays | |
int row=0,col=0; //the row and col | |
int x=0,y=0; //the x and y coordinate | |
void output(int a[][N]) | |
{ | |
for(row=0;row<N;row++) | |
{ | |
for(col=0;col<N;col++) | |
{ | |
printf("%d\t",a[row][col]); | |
} | |
printf("\n"); | |
printf("\n"); | |
}//end of for | |
}//end of output | |
int rowCheck(int a[][N],int row_value, int num)//corrected | |
{ | |
for(col=0;col<N;col++) | |
{ | |
if(a[row_value][col]==num) | |
{ | |
return 0; //cannot be done | |
} | |
} | |
return 1; //is safe | |
}// | |
int colCheck(int a[][N],int col_value,int num)//corrected | |
{ | |
for(row=0;row<N;row++) | |
{ | |
if(a[row][col_value]==num) | |
{ | |
return 0; //cannot be done | |
} | |
} | |
return 1; //is safe | |
}// | |
int boxCheck(int a[][N],int rowStart,int colStart, int num)//corrected | |
{ | |
for (row = 0; row < 3 ; row++) | |
{ | |
for (int col = 0; col < 3; col++) | |
{ | |
if (a[row+rowStart][col+colStart] == num) | |
{ | |
return 0; //not safe | |
} | |
} | |
} | |
return 1; //safe | |
}// | |
int isSafe(int a[][N],int row,int col,int num)//corrected | |
{ | |
if(rowCheck(a,row,num)==1) | |
{ | |
if(colCheck(a,col,num)==1) | |
{ | |
if(boxCheck(a,row-row%3,col-col%3,num)==1) | |
{ | |
return 1; //safe | |
} | |
} | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
int unassign(int a[][N],int *row1,int *col1)//corrected | |
{ | |
for(row=0;row<N;row++) | |
{ | |
for(col=0;col<N;col++) | |
{ | |
if(a[row][col]==unassigned) | |
{ | |
*row1=row; | |
*col1=col; | |
return 1; //unassigned | |
} | |
} | |
} | |
return 0; //all full | |
} | |
int solveSudoku(int a[][N])//corrected | |
{ | |
int row1,col1; | |
if(unassign(a,&row1,&col1)==0)//when all full(corrected) | |
return 1;//success | |
for(int ar=1;ar<=N;ar++) | |
{ | |
if(isSafe(a,row1,col1,ar)==1)//corrected | |
{ | |
a[row1][col1]=ar; | |
if(solveSudoku(a)==1)//corrected | |
return 1; | |
a[row1][col1]=0; | |
} | |
} | |
return 0;//unsuccesfull | |
} | |
int main() | |
{ | |
int a[N][N]= {{0, 0, 0, 0, 1, 0, 0, 0, 0}, | |
{0, 4, 0, 2, 0, 0, 0, 9, 6}, | |
{1, 0, 0, 0, 0, 0, 0, 0, 2}, | |
{0, 7, 0, 5, 0, 0, 8, 2, 0}, | |
{3, 0, 0, 0, 4, 0, 0, 0, 9}, | |
{0, 2, 5, 0, 0, 3, 0, 6, 0}, | |
{4, 0, 0, 0, 0, 9, 0, 0, 8}, | |
{6, 9, 0, 0, 0, 4, 0, 7, 0}, | |
{0, 0, 0, 0, 7, 0, 0, 0, 0}}; | |
// | |
printf("THE INPUT: \n"); | |
output(a); | |
int suc=solveSudoku(a);//corrected | |
// output(); | |
if(suc==1) | |
{ | |
printf("THE OUTPUT: \n"); | |
output(a); | |
} | |
return 0; | |
}//end of the main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment