Algorithms_in_C
1.0.0
Set of algorithms implemented in C.
|
|
struct | sudoku |
| Structure to hold the matrix and dimensions. More...
|
|
|
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...
|
|
◆ 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] | a | pointer to sudoku instance |
[out] | x | pointer to row index of next unknown |
[out] | y | pointer to column index of next unknown |
- Returns
true
if an empty location was found
-
false
if no more empty locations found
146 for (
int i = 0; i < a->N; i++)
148 for (
int j = 0; j < a->N; j++)
150 if (a->a[i * a->N + j] == 0)
◆ 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
-
a | sudoku to check |
x | row to place value |
y | column to place value |
v | value to check if it is valid |
- Returns
true
if valid
-
false
if in-valid
113 bool result =
OKrow(a, x, y, v);
115 result =
OKcol(a, x, y, v);
117 result =
OKbox(a, x, y, v);
◆ OKbox()
bool OKbox |
( |
const struct sudoku * |
a, |
|
|
int |
x, |
|
|
int |
y, |
|
|
int |
v |
|
) |
| |
Check if a 3x3 box is valid.
- Parameters
-
a | matrix to check |
x | row index of the element to check |
y | column index of the element to check |
v | value to check if it repeats |
- Returns
true
if valid
-
false
if in-valid
91 int bi = x - x % a->N2, bj = y - y % a->N2;
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)
◆ OKcol()
bool OKcol |
( |
const struct sudoku * |
a, |
|
|
int |
x, |
|
|
int |
y, |
|
|
int |
v |
|
) |
| |
Check if y
^th column is valid.
- Parameters
-
a | sudoku to check |
x | ignored row |
y | column to check |
v | value to check if it repeats |
- Returns
true
if valid
-
false
if in-valid
69 for (
int i = 0; i < a->N; i++)
70 if (a->a[i * a->N + y] == v)
◆ OKrow()
bool OKrow |
( |
const struct sudoku * |
a, |
|
|
int |
x, |
|
|
int |
y, |
|
|
int |
v |
|
) |
| |
Check if x
^th row is valid.
- Parameters
-
a | sudoku to check |
x | row to check |
y | ignored column |
v | value to check if it repeats |
- Returns
true
if valid
-
false
if in-valid
50 int offset = x * a->N;
51 for (
int j = 0; j < a->N; j++)
52 if (a->a[offset + j] == v)
◆ print()
void print |
( |
const struct sudoku * |
a | ) |
|
Print the matrix to stdout.
- Parameters
-
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' :
' '));
◆ 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] | a | sudoku matrix to solve |
- Returns
true
if solution found
-
false
if no solution found
174 static uint32_t counter = 0;
176 static char prefix[100] =
"";
187 for (uint8_t v = 1; v <= a->N; v++)
189 printf(
"%sTry (%d,%d) = %" SCNu8
"... ", prefix, i, j, v);
195 printf(
"passed (counter=%" SCNu32
")\n", counter);
196 a->a[i * a->N + j] = v;
204 printf(
"%sBacktrack (%d,%d) <- %" SCNu8
" (counter=%" SCNu32
")\n",
205 prefix, i, j, a->a[i * a->N + j], counter);
207 prefix[strlen(prefix) - 2] =
'\0';
208 a->a[i * a->N + j] = 0;
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
bool OKrow(const struct sudoku *a, int x, int y, int v)
Check if x^th row is valid.
Definition: sudoku_solver.c:48
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 OKbox(const struct sudoku *a, int x, int y, int v)
Check if a 3x3 box is valid.
Definition: sudoku_solver.c:85
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