-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.c
More file actions
74 lines (68 loc) · 1.67 KB
/
Copy pathsolver.c
File metadata and controls
74 lines (68 loc) · 1.67 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* solver.c :+: :+: */
/* +:+ */
/* By: fhignett <fhignett@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2019/04/15 18:31:14 by fhignett #+# #+# */
/* Updated: 2020/10/07 14:18:38 by flintlouis ######## odam.nl */
/* */
/* ************************************************************************** */
#include "sudoku.h"
static void get_index(int *j, int *i)
{
if (*i < 8)
*i += 1;
else
{
*i = 0;
*j += 1;
}
}
static void go_back_index(int *j, int *i)
{
if (*i == 0)
{
*i = 8;
*j -= 1;
}
else
*i -= 1;
}
static void backtracking(char **sud)
{
system("clear");
print_sudoku(sud);
// sleep(1);
}
int solver(char **sud, int j, int i, int bt)
{
char nbr = '1';
if (j == 9)
return (1);
if (ft_isdigit(sud[j][i]))
{
get_index(&j, &i);
return (solver(sud, j, i, bt));
}
if (bt)
backtracking(sud); /* <------- to show the backtracking */
while (nbr <= '9')
{
if (checker(sud, j, i, nbr))
{
sud[j][i] = nbr;
get_index(&j, &i);
if (solver(sud, j, i, bt))
return (1);
else
{
go_back_index(&j, &i);
sud[j][i] = '.';
}
}
nbr += 1;
}
return (0);
}