-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo_anayls.c
107 lines (98 loc) · 2.42 KB
/
algo_anayls.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algo_anayls.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebennamr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/04 13:46:30 by ebennamr #+# #+# */
/* Updated: 2023/02/13 16:08:35 by ebennamr ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
static void t_cmd_inint(t_cmd *t1)
{
t1->a_move = -1;
t1->a_type = 0;
t1->b_move = -1;
t1->b_type = 0;
}
static void t_cmd_copy(t_cmd *t1, t_cmd *t2)
{
t1->a_move = t2->a_move;
t1->a_type = t2->a_type;
t1->b_move = t2->b_move;
t1->b_type = t2->b_type;
}
static void search_in_a(t_int2 *stack, t_cmd *t, int num)
{
int i;
int idx;
idx = indexofmax(stack->pt, stack->len);
i = -1;
while (++i < stack->len)
if (stack->pt[i] > num && stack->pt[i] < stack->pt[idx])
idx = i;
if (idx <= (stack->len / 2))
{
t->a_move = idx;
t->a_type = TYPE_RX;
}
else
{
t->a_move = stack->len - idx;
t->a_type = TYPE_RRX;
}
if (indexofmax(stack->pt, stack->len) == idx && num > stack->pt[idx])
{
if (idx <= stack->len / 2)
t->a_move += 1;
else
t->a_move -= 1;
}
}
static int num_of_inst(t_cmd t1)
{
int mv;
mv = 0;
if (t1.a_type == t1.b_type)
{
mv = min(t1.a_move, t1.b_move);
t1.a_move -= mv;
t1.b_move -= mv;
if (t1.a_move > 0)
mv += t1.a_move;
if (t1.b_move > 0)
mv += t1.b_move;
}
else
mv = t1.a_move + t1.b_move;
return (mv);
}
t_cmd searchofbest_move(t_data *data)
{
t_cmd t1;
t_cmd t2;
int i;
t_cmd_inint(&t1);
t_cmd_inint(&t2);
i = 0;
while (i < data->stack_b->len)
{
search_in_a(data->stack_a, &t2, data->stack_b->pt[i]);
if (i <= data->stack_b->len / 2)
{
t2.b_move = i;
t2.b_type = TYPE_RX;
}
else
{
t2.b_move = (data->stack_b->len - i);
t2.b_type = TYPE_RRX;
}
if (t1.a_move < 0 || num_of_inst(t2) < num_of_inst(t1))
t_cmd_copy(&t1, &t2);
i++;
}
return (t1);
}