-
Notifications
You must be signed in to change notification settings - Fork 0
/
sud1.c
70 lines (68 loc) · 1.31 KB
/
sud1.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
66
67
68
69
70
#include<stdio.h>
int puzz[9][9];
int rows(int n,int row)
{ int i;
for (i=0;i<9;i++)
if(puzz[row][i]==n) //check all columns of the row (1 if poss)
return 0;
return 1;
}
int columns(int n,int col)
{ int i;
for (i=0;i<9;i++)
if(puzz[i][col]==n) // check all rows in that column (1 if poss)
return 0;
return 1;
}
int block(int n,int row, int col)
{ int r,c,i,j;
r=(row/3)*3;
c=(col/3)*3;
for(i=r;i<r+3;i++)
for(j=c;j<c+3;j++)
if(puzz[i][j]==n) //check block
return 0;
return 1;
}
void print() //print
{ int i,j;
printf("The Solved Sudoku is \n");
for(i=0;i<9;i++){
for (j=0;j<9;j++)
printf(" %d",puzz[i][j]);
printf("\n");
}
getch();
}
void solve(int row,int col)
{
if(row>8)
print();
if(puzz[row][col]!=0)
{ if(col<8)
solve(row,col+1);
else
solve(row+1,0);
}
else
{ int i;
for (i=1;i<9;i++)
{ if((rows(i,row)&&columns(i,col)&&block(i,row,col))==1){
puzz[row][col]=i; // assign n=x
if(col<8)
solve(row,col+1);
else
solve(row+1,0);
}
}
puzz[row][col]=0;
}
}
int main()
{ int row,column;
printf("Enter the sudoku to be solved and enter 0 for unknown entries\n");
for(row=0;row<9;row++)
for(column=0;column<9;column++)
scanf("%d",&puzz[row][column]);
solve(0,0);
}