-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_rev_b.c
112 lines (103 loc) · 2.63 KB
/
calculate_rev_b.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* calculate_rev_b.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhlim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/09 19:33:30 by zhlim #+# #+# */
/* Updated: 2023/08/11 11:39:48 by zhlim ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void clear_op(t_content *content)
{
content->ra = 0;
content->rb = 0;
content->rr = 0;
content->rra = 0;
content->rrb = 0;
content->rrr = 0;
}
void get_revcost_b2(t_content *content, t_list *stack_b, int size)
{
int i;
int current;
int next_small;
int next_small_idx;
i = 0;
next_small_idx = -1;
while (stack_b)
{
current = get_number(stack_b->content);
if (current < content->number && (next_small_idx == -1
|| current > next_small))
{
next_small = current;
next_small_idx = i;
}
i++;
stack_b = stack_b->next;
}
content->rrb = size - next_small_idx;
}
int get_revcost_b(t_content *content, t_list *stack_b, t_op *op)
{
int size;
size = ft_lstsize(stack_b);
get_big_small_b(stack_b, op);
if (content->number < op->smallest)
{
if (op->big_index == 0)
content->rrb = 0;
else
content->rrb = size - op->big_index;
}
else
get_revcost_b2(content, stack_b, size);
return (content->cost);
}
t_list *move_stack(t_list *stack, t_op *op, int half)
{
op->i = 0;
if (ft_lstsize(stack) % 2 != 0)
{
while (op->i < half + 1)
{
stack = stack->next;
op->i++;
}
}
else
{
while (op->i < half)
{
stack = stack->next;
op->i++;
}
}
return (stack);
}
void calculate_rev_b(t_list *stack_a, t_list *stack_b, t_op *op)
{
t_content content_a;
int half;
half = ft_lstsize(stack_a) / 2;
stack_a = move_stack(stack_a, op, half);
while (stack_a)
{
copy_content(stack_a->content, &content_a, 1);
content_a.rra = half;
op->cost = get_revcost_b(&content_a, stack_b, op);
check_double(&content_a, op);
if (op->cost < op->cheapest)
{
op->cheapest = op->cost;
op->to_push = op->i;
copy_content(&content_a, stack_a->content, 0);
}
op->i++;
half--;
stack_a = stack_a->next;
}
}