-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_build.c
87 lines (78 loc) · 2.13 KB
/
solution_build.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* solution_build.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alex <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/08 13:18:51 by alex #+# #+# */
/* Updated: 2020/05/14 20:14:22 by alex ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/lem_in.h"
static int path_length(t_room *origin)
{
int len;
len = 0;
while (origin != NULL)
{
++len;
origin = origin->succ;
}
return (len);
}
static t_path *init_paths(int n_paths, t_room const *start)
{
t_path *paths;
t_list *links;
t_link *l;
int i;
paths = ft_malloc_or_exit(n_paths * sizeof(t_path));
links = start->links;
i = 0;
while (i < n_paths)
{
l = links->data;
if (l->dst->pred != NULL)
{
paths[i].origin = l->dst;
paths[i].length = path_length(l->dst);
++i;
}
links = links->next;
}
return (paths);
}
static void sort_paths(int n_paths, t_path *paths)
{
int i;
int j;
t_path tmp;
i = 0;
while (i < n_paths - 1)
{
j = i + 1;
while (j < n_paths)
{
if (paths[i].length > paths[j].length)
{
tmp = paths[i];
paths[i] = paths[j];
paths[j] = tmp;
}
++j;
}
++i;
}
}
t_solution *solution_build(int n_ants, t_room const *start, int n_paths)
{
t_solution *solution;
solution = ft_malloc_or_exit(sizeof(t_solution));
solution->n_paths = n_paths;
solution->paths = init_paths(n_paths, start);
sort_paths(n_paths, solution->paths);
solution->ants_per_path = ft_malloc_or_exit(n_paths * sizeof(int));
distribute_ants(n_ants, solution);
return (solution);
}