-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku-solver.c
65 lines (59 loc) · 1.63 KB
/
sudoku-solver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//A program to solve Sudoku puzzles.
//Oct-2020
#include <stdio.h>
#include <stdbool.h>
void input(int sudoku[9][9]){
printf("Enter numbers to be filled as '0'..give space between each number and press enter after each line\n");
printf("Enter your puzzle below \n");
for(int i=0;i<9;i++){
for(int j=0;j<9;j++) scanf("%d",&sudoku[i][j]);
getchar();
}
printf("\n\nThe Solved Puzzle : \n");
}
void output(int sudoku[9][9]){
for(int i=0;i<9;i++){
for(int j=0;j<9;j++) printf(" %d ",sudoku[i][j]);
printf("\n");
}
}
bool safe(int sudoku[9][9],int row,int column,int value){
bool row_check=true,col_check=true,submatrix_check=true;
int i,j;
for(j=0;j<9;j++)
if(sudoku[row][j]==value) row_check=false;
for(i=0;i<9;i++)
if(sudoku[i][column]==value) col_check=false;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(sudoku[i+(row-(row%3))][j+(column-(column%3))]==value)
submatrix_check=false;
if(row_check==true&&col_check==true&&submatrix_check==true)
return true;else return false;
}
bool solve(int sudoku[9][9],int row,int column){
if(row==8&&column==9)return true;
if(column==9){row++;column=0;}
if(sudoku[row][column]>0){++column;return solve(sudoku,row,column);}
for(int value=1;value<=9;value++){
if(safe(sudoku,row,column,value)){
sudoku[row][column]=value;++column;
if(solve(sudoku,row,column))return true;else{
if(column==0){--row;column=8;}
else --column;
}
}
sudoku[row][column]=0;
}
return false;
}
int main(void){
int sudoku[9][9];
input(sudoku);
if(!solve(sudoku,0,0)){
printf("\n\nCan't be Solved!!\n\n");
return 0;
}
output(sudoku);
return 0;
}