Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
Sudoku solver

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. More...
 
bool OKcol (const struct sudoku *a, int x, int y, int v)
 Check if y^th column is valid. More...
 
bool OKbox (const struct sudoku *a, int x, int y, int v)
 Check if a 3x3 box is valid. More...
 
bool OK (const struct sudoku *a, int x, int y, int v)
 Check if element v is valid to place at (x,y) location. More...
 
void print (const struct sudoku *a)
 Print the matrix to stdout. More...
 
bool get_next_unknown (const struct sudoku *a, int *x, int *y)
 Find and get the location for next empty cell. More...
 
bool solve (struct sudoku *a)
 Function to solve a partially filled sudoku matrix. More...
 

Detailed Description

Function Documentation

◆ get_next_unknown()

bool get_next_unknown ( const struct sudoku a,
int *  x,
int *  y 
)

Find and get the location for next empty cell.

Parameters
[in]apointer to sudoku instance
[out]xpointer to row index of next unknown
[out]ypointer to column index of next unknown
Returns
true if an empty location was found
false if no more empty locations found
145 {
146  for (int i = 0; i < a->N; i++)
147  {
148  for (int j = 0; j < a->N; j++)
149  {
150  if (a->a[i * a->N + j] == 0)
151  {
152  *x = i;
153  *y = j;
154  return true;
155  }
156  }
157  }
158 
159  /* no unknown locations found */
160  return false;
161 }

◆ OK()

bool OK ( const struct sudoku a,
int  x,
int  y,
int  v 
)

Check if element v is valid to place at (x,y) location.

Parameters
asudoku to check
xrow to place value
ycolumn to place value
vvalue to check if it is valid
Returns
true if valid
false if in-valid
112 {
113  bool result = OKrow(a, x, y, v);
114  if (result)
115  result = OKcol(a, x, y, v);
116  if (result)
117  result = OKbox(a, x, y, v);
118 
119  return result;
120 }
bool OKbox(const struct sudoku *a, int x, int y, int v)
Check if a 3x3 box is valid.
Definition: sudoku_solver.c:85
bool OKcol(const struct sudoku *a, int x, int y, int v)
Check if y^th column is valid.
Definition: sudoku_solver.c:67
bool OKrow(const struct sudoku *a, int x, int y, int v)
Check if x^th row is valid.
Definition: sudoku_solver.c:48
Here is the call graph for this function:

◆ OKbox()

bool OKbox ( const struct sudoku a,
int  x,
int  y,
int  v 
)

Check if a 3x3 box is valid.

Parameters
amatrix to check
xrow index of the element to check
ycolumn index of the element to check
vvalue to check if it repeats
Returns
true if valid
false if in-valid
86 {
87  /* get start indices of the box that the current (x,y) lies in
88  remember that in C/C++, division operation always rounds towards
89  -infinity for signed integers and towards 0 for unsigned integers
90  */
91  int bi = x - x % a->N2, bj = y - y % a->N2;
92  // printf("Checking box: (%d,%d)\n", bi, bj);
93 
94  for (int i = bi; i < (bi + a->N2); i++)
95  for (int j = bj; j < (bj + a->N2); j++)
96  if (a->a[i * a->N + j] == v)
97  // if the value is found in the box
98  return false;
99  return true;
100 }

◆ OKcol()

bool OKcol ( const struct sudoku a,
int  x,
int  y,
int  v 
)

Check if y^th column is valid.

Parameters
asudoku to check
xignored row
ycolumn to check
vvalue to check if it repeats
Returns
true if valid
false if in-valid
68 {
69  for (int i = 0; i < a->N; i++)
70  if (a->a[i * a->N + y] == v)
71  // if the value is found in the column
72  return false;
73  return true;
74 }

◆ OKrow()

bool OKrow ( const struct sudoku a,
int  x,
int  y,
int  v 
)

Check if x^th row is valid.

Parameters
asudoku to check
xrow to check
yignored column
vvalue to check if it repeats
Returns
true if valid
false if in-valid
49 {
50  int offset = x * a->N;
51  for (int j = 0; j < a->N; j++)
52  if (a->a[offset + j] == v)
53  // if the value is found in the row
54  return false;
55  return true;
56 }

◆ print()

void print ( const struct sudoku a)

Print the matrix to stdout.

Parameters
[in]aarray to print
127 {
128  int i, j;
129  for (i = 0; i < a->N; i++)
130  for (j = 0; j < a->N; j++)
131  printf("%" SCNu8 "%c", a->a[i * a->N + j],
132  (j == a->N - 1 ? '\n' : ' '));
133 }

◆ solve()

bool solve ( struct sudoku a)

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.

Parameters
[in,out]asudoku matrix to solve
Returns
true if solution found
false if no solution found
173 {
174  static uint32_t counter = 0;
175  int i, j;
176  static char prefix[100] = ""; // enough memory
177 
178  if (!get_next_unknown(a, &i, &j))
179  {
180  /* no more empty location found
181  implies all good in the matrix
182  */
183  return true;
184  }
185 
186  /* try all possible values for the unknown */
187  for (uint8_t v = 1; v <= a->N; v++)
188  { /* try all possible values 1 thru N */
189  printf("%sTry (%d,%d) = %" SCNu8 "... ", prefix, i, j, v);
190  counter++;
191  if (OK(a, i, j, v))
192  {
193  /* if assignment checks satisfy, set the value and
194  continue with remaining elements */
195  printf("passed (counter=%" SCNu32 ")\n", counter);
196  a->a[i * a->N + j] = v;
197  strcat(prefix, " ");
198  if (solve(a))
199  {
200  /* solution found */
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'; // truncate the prefix
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
Here is the call graph for this function: