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.
173{
174 static uint32_t counter = 0;
175 int i, j;
176 static char prefix[100] = "";
177
179 {
180
181
182
183 return true;
184 }
185
186
187 for (uint8_t v = 1; v <= a->N; v++)
188 {
189 printf("%sTry (%d,%d) = %" SCNu8 "... ", prefix, i, j, v);
190 counter++;
192 {
193
194
195 printf("passed (counter=%" SCNu32 ")\n", counter);
196 a->a[i * a->N + j] = v;
197 strcat(prefix, " ");
199 {
200
201 return true;
202 }
203
204 printf("%sBacktrack (%d,%d) <- %" SCNu8 " (counter=%" SCNu32 ")\n",
205 prefix, i, j, a->a[i * a->N + j], counter);
206
207 prefix[strlen(prefix) - 2] = '\0';
208 a->a[i * a->N + j] = 0;
209 }
210 else
211 {
212 printf("\r");
213 }
214 }
215
216 return false;
217}
bool OK(const struct sudoku *a, int x, int y, int v)
Check if element v is valid to place at (x,y) location.
Definition: sudoku_solver.c:111
bool get_next_unknown(const struct sudoku *a, int *x, int *y)
Find and get the location for next empty cell.
Definition: sudoku_solver.c:144
bool solve(struct sudoku *a)
Function to solve a partially filled sudoku matrix.
Definition: sudoku_solver.c:172