This repository has been archived by the owner on Feb 27, 2021. It is now read-only.
forked from giacomoguiulfo/42-fillit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.c
134 lines (124 loc) · 2.84 KB
/
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* solve.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkalia <jkalia@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/09 18:10:56 by jkalia #+# #+# */
/* Updated: 2017/03/11 10:18:29 by jkalia ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
void remove_tetri(char **map, char *tetri, int col, int row)
{
char ch;
int i;
ch = get_letter(tetri);
i = 0;
while (map[row])
{
col = 0;
while (map[row][col])
{
if (i == 4)
return ;
if (map[row][col] == ch)
{
i++;
map[row][col] = '.';
}
col++;
}
row++;
}
}
void place(char **map, char *tetri, int col, int row)
{
size_t i;
int init_col;
init_col = col;
i = 0;
while (*tetri == '.')
DO3(i++, tetri++, init_col--);
while (*tetri != '\0')
{
if (i > 3)
{
i = 0;
col = init_col;
row++;
}
if (*tetri == '.')
DO2(i++, col++);
if (DOT(map[row][col]) && !DOT(*tetri))
DO3(map[row][col] = *tetri, col++, i++);
tetri++;
}
}
t_bool is_safe(char **map, char *tetri, int col, int row)
{
size_t i;
int init_col;
init_col = col;
i = 0;
while (*tetri == '.')
DO3(i++, tetri++, init_col--);
CHK(init_col < 0, false);
while (*tetri != '\0')
{
if (i > 3)
{
i = 0;
col = init_col;
row++;
}
if (*tetri == '.')
DO2(i++, col++);
CHK(!DOT(map[row][col]) && map[row][col] && !DOT(*tetri), false);
CHK(!map[row][col] && !DOT(*tetri), false);
if (DOT(map[row][col]) && !DOT(*tetri))
DO2(col++, i++);
tetri++;
}
return (true);
}
int solve(char **tbl, size_t blocks)
{
char **map;
size_t map_size;
map_size = initial_board_size(blocks);
CHK1((map = new_map(map_size)) == 0, error(), 0);
while (recursion(tbl, map, 0, 0) == false)
{
map_size++;
delete_map(map);
CHK1((map = new_map(map_size)) == 0, error(), 0);
}
print_map(map, map_size);
delete_map(map);
return (0);
}
t_bool recursion(char **tbl, char **map, int col, int row)
{
if (!*tbl)
return (true);
while (map[row])
{
while (map[row][col])
{
if (is_safe(map, *tbl, col, row) == true)
{
place(map, *tbl, col, row);
if (recursion(tbl + 1, map, 0, 0) == true)
return (true);
else
remove_tetri(map, *tbl, col, row);
}
col++;
}
row++;
col = 0;
}
return (false);
}