Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sudoku_solver.c File Reference

Sudoku Solver using recursive implementation of brute-force algorithm. More...

#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for sudoku_solver.c:

Data Structures

struct  sudoku
 Structure to hold the matrix and dimensions. More...
 

Functions

bool OKrow (const struct sudoku *a, int x, int y, int v)
 Check if x^th row is valid.
 
bool OKcol (const struct sudoku *a, int x, int y, int v)
 Check if y^th column is valid.
 
bool OKbox (const struct sudoku *a, int x, int y, int v)
 Check if a 3x3 box is valid.
 
bool OK (const struct sudoku *a, int x, int y, int v)
 Check if element v is valid to place at (x,y) location.
 
void print (const struct sudoku *a)
 Print the matrix to stdout.
 
bool get_next_unknown (const struct sudoku *a, int *x, int *y)
 Find and get the location for next empty cell.
 
bool solve (struct sudoku *a)
 Function to solve a partially filled sudoku matrix.
 
void test ()
 
int main ()
 Main function.
 

Detailed Description

Sudoku Solver using recursive implementation of brute-force algorithm.

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
Krishna Vedala

Function Documentation

◆ main()

int main ( void  )

Main function.

247{
248 test();
249
250 struct sudoku a; // store the matrix as a 1D array
251 scanf("%" SCNu8, &(a.N));
252 a.a = (uint8_t *)malloc(a.N * a.N * sizeof(uint8_t));
253 a.N2 = (uint8_t)sqrt(a.N);
254
255 for (int i = 0; i < a.N; i++)
256 for (int j = 0; j < a.N; j++) scanf("%" SCNu8, &(a.a[i * a.N + j]));
257
258 printf("Entered a %udx%ud matrix with block size: %" SCNu8 "\n", a.N, a.N,
259 a.N2);
260 // print(&a);
261 printf("\n\n");
262 if (solve(&a))
263 printf("Valid solution found!\n");
264 else
265 printf("Invalid\n");
266 print(&a);
267
268 free(a.a);
269 return 0;
270}
void test()
Test function.
Definition: decimal_to_binary_recursion.c:20
void print(const struct sudoku *a)
Print the matrix to stdout.
Definition: sudoku_solver.c:126
bool solve(struct sudoku *a)
Function to solve a partially filled sudoku matrix.
Definition: sudoku_solver.c:172
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
Structure to hold the matrix and dimensions.
Definition: sudoku_solver.c:33
uint8_t * a
matrix as a flattened 1D row-major array
Definition: sudoku_solver.c:34
Here is the call graph for this function:

◆ test()

void test ( void  )
222{
223 printf("Test begin...\n");
224
225 uint8_t test_array[] = {3, 0, 6, 5, 0, 8, 4, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0,
226 0, 0, 8, 7, 0, 0, 0, 0, 3, 1, 0, 0, 3, 0, 1, 0, 0,
227 8, 0, 9, 0, 0, 8, 6, 3, 0, 0, 5, 0, 5, 0, 0, 9, 0,
228 6, 0, 0, 1, 3, 0, 0, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0,
229 0, 0, 7, 4, 0, 0, 5, 2, 0, 6, 3, 0, 0};
230 struct sudoku a = {.N = 9, .N2 = 3, .a = test_array};
231 assert(solve(&a)); // ensure that solution is obtained
232
233 uint8_t expected[] = {3, 1, 6, 5, 7, 8, 4, 9, 2, 5, 2, 9, 1, 3, 4, 7, 6,
234 8, 4, 8, 7, 6, 2, 9, 5, 3, 1, 2, 6, 3, 4, 1, 5, 9,
235 8, 7, 9, 7, 4, 8, 6, 3, 1, 2, 5, 8, 5, 1, 7, 9, 2,
236 6, 4, 3, 1, 3, 8, 9, 4, 7, 2, 5, 6, 6, 9, 2, 3, 5,
237 1, 8, 7, 4, 7, 4, 5, 2, 8, 6, 3, 1, 9};
238 for (int i = 0; i < a.N; i++)
239 for (int j = 0; j < a.N; j++)
240 assert(a.a[i * a.N + j] == expected[i * a.N + j]);
241
242 printf("Test passed\n");
243}