-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
sudoku_solver.c
270 lines (244 loc) · 7.51 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* @file
* @brief Sudoku Solver using recursive implementation of brute-force algorithm
*
* @details
* Given an incomplete N*N Sudoku and asked to solve it using the
* following recursive algorithm:
* 1. Scan the Sudoku from left to right row-wise to search for an empty cell.
* 2. If there are no empty cells, print the Sudoku. Go to step 5.
* 3. In the empty cell, try putting numbers 1 to N
* while ensuring that no two numbers in a single row, column, or box are same.
* Go back to step 1.
* 4. Declare that the Sudoku is Invalid.
* 5. Exit.
*
* @authors [Anuj Shah](https://github.com/anujms1999)
* @authors [Krishna Vedala](https://github.com/kvedala)
*/
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/** @addtogroup sudoku Sudoku solver
* @{
*/
/** Structure to hold the matrix and dimensions
*/
struct sudoku
{
uint8_t *a; /**< matrix as a flattened 1D row-major array */
uint8_t N; /**< number of elements */
uint8_t N2; /**< block of elements */
};
/**
* Check if `x`^th row is valid
* @param a ::sudoku to check
* @param x row to check
* @param y ignored column
* @param v value to check if it repeats
* @returns `true` if valid
* @returns `false` if in-valid
*/
bool OKrow(const struct sudoku *a, int x, int y, int v)
{
int offset = x * a->N;
for (int j = 0; j < a->N; j++)
if (a->a[offset + j] == v)
// if the value is found in the row
return false;
return true;
}
/**
* Check if `y`^th column is valid
* @param a ::sudoku to check
* @param x ignored row
* @param y column to check
* @param v value to check if it repeats
* @returns `true` if valid
* @returns `false` if in-valid
*/
bool OKcol(const struct sudoku *a, int x, int y, int v)
{
for (int i = 0; i < a->N; i++)
if (a->a[i * a->N + y] == v)
// if the value is found in the column
return false;
return true;
}
/**
* Check if a 3x3 box is valid
* @param a matrix to check
* @param x row index of the element to check
* @param y column index of the element to check
* @param v value to check if it repeats
* @returns `true` if valid
* @returns `false` if in-valid
*/
bool OKbox(const struct sudoku *a, int x, int y, int v)
{
/* get start indices of the box that the current (x,y) lies in
remember that in C/C++, division operation always rounds towards
-infinity for signed integers and towards 0 for unsigned integers
*/
int bi = x - x % a->N2, bj = y - y % a->N2;
// printf("Checking box: (%d,%d)\n", bi, bj);
for (int i = bi; i < (bi + a->N2); i++)
for (int j = bj; j < (bj + a->N2); j++)
if (a->a[i * a->N + j] == v)
// if the value is found in the box
return false;
return true;
}
/**
* Check if element `v` is valid to place at (x,y) location.
* @param a ::sudoku to check
* @param x row to place value
* @param y column to place value
* @param v value to check if it is valid
* @returns `true` if valid
* @returns `false` if in-valid
*/
bool OK(const struct sudoku *a, int x, int y, int v)
{
bool result = OKrow(a, x, y, v);
if (result)
result = OKcol(a, x, y, v);
if (result)
result = OKbox(a, x, y, v);
return result;
}
/**
* Print the matrix to stdout
* @param [in] a array to print
*/
void print(const struct sudoku *a)
{
int i, j;
for (i = 0; i < a->N; i++)
for (j = 0; j < a->N; j++)
printf("%" SCNu8 "%c", a->a[i * a->N + j],
(j == a->N - 1 ? '\n' : ' '));
}
/**
* @brief Find and get the location for next empty cell.
*
* @param [in] a pointer to sudoku instance
* @param [out] x pointer to row index of next unknown
* @param [out] y pointer to column index of next unknown
* @returns `true` if an empty location was found
* @returns `false` if no more empty locations found
*/
bool get_next_unknown(const struct sudoku *a, int *x, int *y)
{
for (int i = 0; i < a->N; i++)
{
for (int j = 0; j < a->N; j++)
{
if (a->a[i * a->N + j] == 0)
{
*x = i;
*y = j;
return true;
}
}
}
/* no unknown locations found */
return false;
}
/**
* @brief Function to solve a partially filled sudoku matrix. For each unknown
* value (0), the function fills a possible value and calls the function again
* to check forvalid solution.
*
* @param [in,out] a sudoku matrix to solve
* @return `true` if solution found
* @return `false` if no solution found
*/
bool solve(struct sudoku *a)
{
static uint32_t counter = 0;
int i, j;
static char prefix[100] = ""; // enough memory
if (!get_next_unknown(a, &i, &j))
{
/* no more empty location found
implies all good in the matrix
*/
return true;
}
/* try all possible values for the unknown */
for (uint8_t v = 1; v <= a->N; v++)
{ /* try all possible values 1 thru N */
printf("%sTry (%d,%d) = %" SCNu8 "... ", prefix, i, j, v);
counter++;
if (OK(a, i, j, v))
{
/* if assignment checks satisfy, set the value and
continue with remaining elements */
printf("passed (counter=%" SCNu32 ")\n", counter);
a->a[i * a->N + j] = v;
strcat(prefix, " ");
if (solve(a))
{
/* solution found */
return true;
}
printf("%sBacktrack (%d,%d) <- %" SCNu8 " (counter=%" SCNu32 ")\n",
prefix, i, j, a->a[i * a->N + j], counter);
prefix[strlen(prefix) - 2] = '\0'; // truncate the prefix
a->a[i * a->N + j] = 0;
}
else
{
printf("\r");
}
}
return false;
}
/** @} */
void test()
{
printf("Test begin...\n");
uint8_t test_array[] = {3, 0, 6, 5, 0, 8, 4, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0,
0, 0, 8, 7, 0, 0, 0, 0, 3, 1, 0, 0, 3, 0, 1, 0, 0,
8, 0, 9, 0, 0, 8, 6, 3, 0, 0, 5, 0, 5, 0, 0, 9, 0,
6, 0, 0, 1, 3, 0, 0, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0,
0, 0, 7, 4, 0, 0, 5, 2, 0, 6, 3, 0, 0};
struct sudoku a = {.N = 9, .N2 = 3, .a = test_array};
assert(solve(&a)); // ensure that solution is obtained
uint8_t expected[] = {3, 1, 6, 5, 7, 8, 4, 9, 2, 5, 2, 9, 1, 3, 4, 7, 6,
8, 4, 8, 7, 6, 2, 9, 5, 3, 1, 2, 6, 3, 4, 1, 5, 9,
8, 7, 9, 7, 4, 8, 6, 3, 1, 2, 5, 8, 5, 1, 7, 9, 2,
6, 4, 3, 1, 3, 8, 9, 4, 7, 2, 5, 6, 6, 9, 2, 3, 5,
1, 8, 7, 4, 7, 4, 5, 2, 8, 6, 3, 1, 9};
for (int i = 0; i < a.N; i++)
for (int j = 0; j < a.N; j++)
assert(a.a[i * a.N + j] == expected[i * a.N + j]);
printf("Test passed\n");
}
/** \brief Main function */
int main()
{
test();
struct sudoku a; // store the matrix as a 1D array
scanf("%" SCNu8, &(a.N));
a.a = (uint8_t *)malloc(a.N * a.N * sizeof(uint8_t));
a.N2 = (uint8_t)sqrt(a.N);
for (int i = 0; i < a.N; i++)
for (int j = 0; j < a.N; j++) scanf("%" SCNu8, &(a.a[i * a.N + j]));
printf("Entered a %udx%ud matrix with block size: %" SCNu8 "\n", a.N, a.N,
a.N2);
// print(&a);
printf("\n\n");
if (solve(&a))
printf("Valid solution found!\n");
else
printf("Invalid\n");
print(&a);
free(a.a);
return 0;
}