-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsudoku.c
197 lines (184 loc) · 3.11 KB
/
sudoku.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
C PROGRAM TO SOLVE SUDOKU
AUTHOR: SWAPNIL SAXENA
COMPUTER SCIENCE AND ENGINEERING
JAYPEE INSTITUTE OF INFORMATION TECHNOLOGY
*/
#include<stdio.h>
#include<stdlib.h>
#define and &&
#define or ||
int **mainCopy;
int **copyCopy;
void concierge();
void reset();
int checkRow(int r, int n);
int checkCol(int c, int n);
int checkGrid(int r, int c, int n);
void show(int **a);
int sudoku(int a);
void main()
{
concierge();
for(int i = 0;i<9;i++)
{
for(int j = 0;j<9;j++)
{
scanf("%d",mainCopy[i]+j);
}
}
reset();
show(mainCopy);
sudoku(0);
printf("\n\n");
show(copyCopy);
}
void concierge()
{
mainCopy = (int **)calloc(sizeof(int *),9);
copyCopy = (int **)calloc(sizeof(int *),9);
for(int i =0; i<9;i++)
{
mainCopy[i] = (int *)calloc(sizeof(int),9);
copyCopy[i] = (int *)calloc(sizeof(int),9);
}
}
void reset()
{
for(int i = 0;i<9;i++)
{
for(int j = 0;j<9;j++)
{
copyCopy[i][j] = mainCopy[i][j];
}
}
}
int checkRow(int r, int n)
{
for(int i = 0;i<9;i++)
{
if (copyCopy[r][i] == n)
{return 0;}
}
return 1;
}
int checkCol(int c, int n)
{
for(int i = 0;i<9;i++)
{
if (copyCopy[i][c] == n)
{return 0;}
}
return 1;
}
int checkGrid(int a, int b, int n) // row,column
{
int r = 0;
int c = 0;
switch(a)
{
case 0:
case 1:
case 2: r = 0; break;
case 3:
case 4:
case 5: r = 3; break;
case 6:
case 7:
case 8: r = 6; break;
}
switch(b)
{
case 0:
case 1:
case 2: c = 0; break;
case 3:
case 4:
case 5: c = 3; break;
case 6:
case 7:
case 8: c = 6; break;
}
for(int i=r;i<(r+3);i++)
{
for(int j=c;j<(c+3);j++)
{
if (copyCopy[i][j] == n)
{return 0;}
}
}
return 1;
}
void show(int **ptr)
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
printf("%d ",ptr[i][j]);
}
printf("\n");
}
}
int sudoku(int a)
{
//printf("\nFUNCTION CALL->%d\n",a);
/*if(a>70)
{show(copyCopy);}*/
int c = a%9;
int r = a/9;
int status = 0;
int i = 0;
if(copyCopy[r][c]!=0)
{
//check if board ends with a constant
if((a+1)==81)
{
status = 1;
return status;
}
// already set data
status = sudoku(a+1);
return status;
}
// check possibilites
for(i = 1; i < 10; i++)
{
if((checkRow(r,i))and(checkCol(c,i))and(checkGrid(r,c,i)))
{
//the code runs if no duplicacy found
//printf("POSSIBILITY for r=%d c=%d n=%d\n",r,c,i );
copyCopy[r][c] = i;
if((a+1) != 81)
{
//printf("HERE %d \n",a+1);
status = sudoku(a+1);
}
else
{
//the code runs if the board ends
//printf("DONE\n");
status = 1;
return status;
}
}
else{continue;}
//if wrong assumption the status is zero hence reset the move
if(!status)
{
//printf("WRONG ASSUMPTION %d %d\n",r,c);
// reset he element
copyCopy[r][c] = mainCopy[r][c];
continue;
}
else{return status;}
}
if (i == 10)
{
// wrong assumption
//printf("WRONG ASSUMPTION AFTER FULL LOOP r=%d c=%d\n",r,c);
status = 0;
return status;
}
return status;
}