/
backtracking.c
144 lines (129 loc) · 2.32 KB
/
backtracking.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "fillit.h"
static char **make_square(size_t lenght)
{
char **square;
size_t i;
size_t j;
if ((square = (char **)malloc(sizeof(char *) * lenght + 1)) == NULL)
ft_error("Malloc fail.");
square[lenght] = NULL;
i = 0;
while (i < lenght)
{
if ((square[i] = (char *)malloc(sizeof(char) * lenght + 1)) == NULL)
ft_error("Malloc fail.");
square[i][lenght] = '\0';
j = 0;
while (j < lenght)
{
square[i][j] = '.';
j++;
}
i++;
}
return (square);
}
static void display(char **square)
{
int i;
i = 0;
while(square[i])
{
ft_putendl(square[i]);
i++;
}
ft_putendl("");
}
char **remove_jeton(char **square, char id)
{
int i;
int j;
i = 0;
while (square[i])
{
j = 0;
while(square[i][j])
{
if (square[i][j] == id)
square[i][j] = '.';
j++;
}
i++;
}
return (square);
}
int set_a_jeton(char **square, struct s_list **list, int i, int j)
{
int coord;
coord = 0;
// segfault possible j negatif ou superieur a la map idem pour i
while (coord < 4)
{
if (square[i][j] == '.')
square[i][j] = (*list)->id;
else
{
square = remove_jeton(square, (*list)->id);
return (-1);
}
coord++;
i += (*list)->x[coord] - (*list)->x[coord - 1];
j += (*list)->y[coord] - (*list)->y[coord - 1];
}
(*list)->flag = 1;
return (0);
}
int check_list(struct s_list *list)
{
int i;
i = 0;
while (i < 4)
{
printf("list flag = %d\n", list->flag);
if (list->flag == 0)
return (-1);
list = list->next;
i++;
}
return (0);
}
void brute_force(struct s_list *list, char **square)
{
int i;
int j;
i = 0;
while (square[i])
{
j = 0;
while (square[i][j])
{
if (square[i][j] == '.')
{
/*
while (list->flag == 1)
{
printf("on continue");
list = list->next;
continue ;
}*/
if (list->flag == 0 && set_a_jeton(square, &list, i, j) == 0)
{
list = list->next;
display(square); // a enlever
}
}
j++;
}
i++;
}
if (check_list(list) == -1)
brute_force(list, square);
}
void backtracking(struct s_list *list)
{
char **square;
(void)*list;
square = make_square(DEFAULT);
display(square);
brute_force(list, square);
}