-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_solve.c
85 lines (76 loc) · 1.88 KB
/
ft_solve.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_solve.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* Tg: @elshirak +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/09 23:26:49 by ELSHIRAK #+# #+# */
/* Updated: 2022/10/09 23:28:27 by ELSHIRAK ### ########.fr */
/* */
/* ************************************************************************** */
#include "lib.h"
void ft_copy_row(char **board, int permutation, int row) // function that fills matrix with rows
{
int i;
i = 4;
while (i > 0)
{
board[row][i] = permutation % 10;
permutation /= 10;
i --;
}
}
int ft_solve_r(char **board, int *perms, int row) // recursively solving board
{
int i;
i = 0;
while (i < 24)
{
ft_copy_row(board, perms[i], row);
if (row < 4)
{
if (ft_solve_r(board, perms, row + 1))
return (1);
}
else
{
if (check_solution(board))
return (1);
}
i++;
}
return (0);
}
int get_permutations(int *perms) // getting non-repeatable strings
{
int i;
int num;
int count;
unsigned char check;
i = 1234;
count = 0;
while (i <= 4321)
{
num = i;
check = 0;
while (num)
{
check ^= 1 << (num % 10);
num /= 10;
}
if (check == 30) // 00011110 = 30
{
perms[count] = i;
count ++;
}
i++;
}
return (count);
}
int ft_solve(char **board)
{
int perms[24];
get_permutations(perms);
return (ft_solve_r(board, perms, 1));
}