-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.c
105 lines (96 loc) · 2.39 KB
/
solver.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* solver.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kmira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/13 17:30:15 by kmira #+# #+# */
/* Updated: 2019/05/20 14:57:15 by kmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
t_tetrimino *g_place_pruning[27] =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
int count_pieces(t_tetrimino *tetrimino)
{
int result;
result = 0;
while (result <= 26 && tetrimino[result].id != 0)
result++;
return (result);
}
#define PIECE_MASK (*(u_int64_t *)(tetrimino[current_piece].mask) >> col)
int solve_board
(t_tetrimino *tetrimino, unsigned short *board, int board_size)
{
int row;
int col;
int current_piece;
row = 0;
col = -1;
current_piece = 0;
while (current_piece >= 0)
{
while (row + tetrimino[current_piece].height <= board_size)
{
while (++col + tetrimino[current_piece].width <= board_size)
if (((PIECE_MASK) & *(u_int64_t *)&board[row]) == 0)
{
place_piece(&tetrimino[current_piece], board, &row, &col);
if (board_is_solved(¤t_piece, tetrimino, &row, &col))
return (1);
}
col = -1;
row++;
}
if (--current_piece >= 0)
remove_piece(tetrimino[current_piece], board, &row, &col);
}
return (-1);
}
int fill_board_with(t_tetrimino *tetrimino)
{
unsigned short board[16];
int board_size;
int pieces;
board_size = 0;
ft_bzero(board, sizeof(board));
pieces = count_pieces(tetrimino);
while (board_size * board_size < pieces * 4)
board_size++;
while (solve_board(tetrimino, board, board_size) == -1)
{
ft_bzero(board, sizeof(board));
board_size++;
}
return (board_size);
}