-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcost.c
65 lines (59 loc) · 1.88 KB
/
cost.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cost.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erigolon <erigolon@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/06 12:05:23 by erigolon #+# #+# */
/* Updated: 2023/06/12 15:11:58 by erigolon ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/push_swap.h"
void cost(t_stack **stack_a, t_stack **stack_b)
{
t_stack *temp_a;
t_stack *temp_b;
int size_a;
int size_b;
temp_a = *stack_a;
temp_b = *stack_b;
size_a = lstsize_stack(*stack_a);
size_b = lstsize_stack(*stack_b);
while (temp_b)
{
temp_b->cost_b = temp_b->pos;
if (temp_b->pos > size_b / 2)
temp_b->cost_b = (size_b - temp_b->pos) * -1;
temp_b->cost_a = temp_b->target_pos;
if (temp_b->target_pos > size_a / 2)
temp_b->cost_a = (size_a - temp_b->target_pos) * -1;
temp_b = temp_b->next;
}
}
int abs_n(int n)
{
if (n < 0)
return (n * -1);
return (n);
}
void cheap_move(t_stack **stack_a, t_stack **stack_b)
{
t_stack *temp;
int cheap;
int cost_a;
int cost_b;
temp = *stack_b;
cheap = INT_MAX;
while (temp)
{
if (abs_n(temp->cost_a) + abs_n(temp->cost_b) < abs_n(cheap))
{
cheap = abs_n(temp->cost_a) + abs_n(temp->cost_b);
cost_a = temp->cost_a;
cost_b = temp->cost_b;
}
temp = temp->next;
}
do_move(stack_a, stack_b, cost_a, cost_b);
}