Memory leak is fixed
This commit is contained in:
parent
12fc9b3108
commit
46387d1666
3 changed files with 46 additions and 59 deletions
|
@ -87,7 +87,7 @@ t_node *add_node_with_coord(t_node *col, t_coord row, int type);
|
||||||
|
|
||||||
t_node **add_cols(t_node *root, int number);
|
t_node **add_cols(t_node *root, int number);
|
||||||
|
|
||||||
void print_solution(t_list *sol, t_node *root, int amount);
|
void print_solution(t_node **sol, int amount, int size);
|
||||||
|
|
||||||
void add_rows(int *types, int amount, int size, t_node **cols_arr);
|
void add_rows(int *types, int amount, int size, t_node **cols_arr);
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ void link_row(int *col_nums, t_node **cols_arr);
|
||||||
|
|
||||||
void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type);
|
void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type);
|
||||||
|
|
||||||
void search(t_node* root, t_list *solution, int k, int amount);
|
void search(t_node* root, t_node **solution, int k, int amount);
|
||||||
|
|
||||||
void cover(t_node *to_cover);
|
void cover(t_node *to_cover);
|
||||||
|
|
||||||
|
|
|
@ -148,8 +148,8 @@ void add_rows(int *types, int amount, int size, t_node **cols_arr)
|
||||||
|
|
||||||
void cover(t_node *to_cover)
|
void cover(t_node *to_cover)
|
||||||
{
|
{
|
||||||
t_node *step_vert;
|
static t_node *step_vert;
|
||||||
t_node *step_horiz;
|
static t_node *step_horiz;
|
||||||
|
|
||||||
to_cover->left->right = to_cover->right;
|
to_cover->left->right = to_cover->right;
|
||||||
to_cover->right->left = to_cover->left;
|
to_cover->right->left = to_cover->left;
|
||||||
|
@ -168,8 +168,8 @@ void cover(t_node *to_cover)
|
||||||
|
|
||||||
void uncover(t_node *to_uncover)
|
void uncover(t_node *to_uncover)
|
||||||
{
|
{
|
||||||
t_node *step_vert;
|
static t_node *step_vert;
|
||||||
t_node *step_horiz;
|
static t_node *step_horiz;
|
||||||
|
|
||||||
step_vert = to_uncover->up;
|
step_vert = to_uncover->up;
|
||||||
while (step_vert != to_uncover)
|
while (step_vert != to_uncover)
|
||||||
|
@ -203,59 +203,46 @@ void fill_map(char *map, int size, t_node *ttr, int letter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_solution(t_list *sol, t_node *root, int amount)
|
void print_solution(t_node **solution, int amount, int size)
|
||||||
{
|
{
|
||||||
// doesn't work for one ttr
|
char *map;
|
||||||
// if ttr has dimensions 3x2
|
int map_size;
|
||||||
// output square must be 3x3
|
int i;
|
||||||
// but func print 4x4
|
|
||||||
|
|
||||||
char *map;
|
map_size = size * size + 1;
|
||||||
int i;
|
map = malloc(sizeof(char) * map_size);
|
||||||
int map_size;
|
map[map_size] = 0;
|
||||||
|
i = 0;
|
||||||
map_size = root->size * root->size + 1;
|
while (i++ < amount)
|
||||||
map = malloc(sizeof(char) * map_size);
|
fill_map(map, size, solution[i - 1], i);
|
||||||
// init with zeroes by ft_memalloc
|
i = 0;
|
||||||
// or in some other way.
|
while (i < map_size - 1)
|
||||||
// map can contain garbage
|
{
|
||||||
map[map_size] = 0;
|
if (map[i] >= 'A' && map[i] <= 'Z')
|
||||||
while (sol)
|
ft_putchar(map[i]);
|
||||||
{
|
else
|
||||||
fill_map(map, root->size, (*(t_node **)sol->content), amount);
|
ft_putchar('.');
|
||||||
sol = sol->next;
|
if ((i + 1) % size == 0)
|
||||||
amount--;
|
ft_putchar('\n');
|
||||||
}
|
i++;
|
||||||
i = 0;
|
}
|
||||||
while (i < root->size * root->size)
|
exit(0);
|
||||||
{
|
|
||||||
if (map[i] >= 'A' && map[i] <= 'Z')
|
|
||||||
ft_putchar(map[i]);
|
|
||||||
else
|
|
||||||
ft_putchar('.');
|
|
||||||
if ((i + 1) % root->size == 0)
|
|
||||||
ft_putchar('\n');
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void search(t_node* root, t_list *solution, int k, int amount)
|
void search(t_node* root,t_node **solution, int k, int amount)
|
||||||
{
|
{
|
||||||
t_node *current_col;
|
t_node *current_col;
|
||||||
t_node *row;
|
t_node *row;
|
||||||
t_node *j;
|
t_node *j;
|
||||||
|
|
||||||
if (k == amount)
|
if (k == amount)
|
||||||
print_solution(solution, root, amount);
|
print_solution(solution, amount, root->size);
|
||||||
if (root->right == root)
|
|
||||||
return ;
|
|
||||||
current_col = root->right;
|
current_col = root->right;
|
||||||
cover(current_col);
|
cover(current_col);
|
||||||
row = current_col->down;
|
row = current_col->down;
|
||||||
while (row != current_col)
|
while (row != current_col)
|
||||||
{
|
{
|
||||||
ft_lstadd(&solution, ft_lstnew(&row, sizeof(t_node *)));
|
solution[k] = row;
|
||||||
j = row->right;
|
j = row->right;
|
||||||
while (j != row)
|
while (j != row)
|
||||||
{
|
{
|
||||||
|
@ -263,8 +250,6 @@ void search(t_node* root, t_list *solution, int k, int amount)
|
||||||
j = j->right;
|
j = j->right;
|
||||||
}
|
}
|
||||||
search(root, solution, k + 1, amount);
|
search(root, solution, k + 1, amount);
|
||||||
row = *((t_node**)(ft_lstpop(&solution)->content));
|
|
||||||
current_col = row->column;
|
|
||||||
j = row->left;
|
j = row->left;
|
||||||
while (j != row)
|
while (j != row)
|
||||||
{
|
{
|
||||||
|
@ -275,4 +260,4 @@ void search(t_node* root, t_list *solution, int k, int amount)
|
||||||
}
|
}
|
||||||
uncover(current_col);
|
uncover(current_col);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
18
src/main.c
18
src/main.c
|
@ -50,7 +50,7 @@ int main(int argc, char **argv)
|
||||||
int size;
|
int size;
|
||||||
t_node *r;
|
t_node *r;
|
||||||
t_node **cols_arr;
|
t_node **cols_arr;
|
||||||
t_list *ans;
|
t_node **ans;
|
||||||
|
|
||||||
ans = NULL;
|
ans = NULL;
|
||||||
i = 0;
|
i = 0;
|
||||||
|
@ -83,14 +83,16 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
types = get_types(ttr);
|
types = get_types(ttr);
|
||||||
amount = get_amount(ttr);
|
amount = get_amount(ttr);
|
||||||
|
ans = (t_node**)malloc(sizeof(t_node*) * amount);
|
||||||
size = ft_sqrt_ceil(amount * 4);
|
size = ft_sqrt_ceil(amount * 4);
|
||||||
while (1) {
|
while (1)
|
||||||
r = init_root(size);
|
{
|
||||||
cols_arr = add_cols(r, (size * size + amount));
|
r = init_root(size);
|
||||||
add_rows(types, amount, size, cols_arr);
|
cols_arr = add_cols(r, (size * size + amount));
|
||||||
search(r, ans, 0, amount);
|
add_rows(types, amount, size, cols_arr);
|
||||||
size++;
|
search(r, ans, 0 ,amount);
|
||||||
}
|
size++;
|
||||||
|
}
|
||||||
//print_ans(ans);
|
//print_ans(ans);
|
||||||
//printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y);
|
//printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y);
|
||||||
//to_letters(ttr);
|
//to_letters(ttr);
|
||||||
|
|
Loading…
Reference in a new issue