diff --git a/.gitignore b/.gitignore index ed17cff..d0ca43c 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ # Clion files .idea +cmake-build-debug diff --git a/CMakeLists.txt b/CMakeLists.txt index 41f9715..b52e1ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,6 @@ include_directories(inc) include_directories(libft) set(SOURCE_FILES - inc/fillit.h libft/ft_atoi.c libft/ft_bzero.c libft/ft_isalnum.c diff --git a/inc/fillit.h b/inc/fillit.h index 0436fa2..f26a3f0 100644 --- a/inc/fillit.h +++ b/inc/fillit.h @@ -21,7 +21,6 @@ #include // warning! # define BUF_S 8192 -# define HOW_MUCH 10000 typedef struct s_coord { @@ -45,7 +44,9 @@ typedef struct s_node struct s_node *down; struct s_node *column; t_coord coord; -} t_node; + int type; + int size; +} t_node; extern t_ttrmn g_templates[19]; @@ -74,10 +75,7 @@ void test_check(char **ttr, t_ttrmn *tamplates); // obsolete, rewrite for structs array void to_letters(char **ttr); -// create array of ttr structures -int *to_strct_array(char **ttr, t_ttrmn *templates); - -t_node *init_root(void); +t_node *init_root(int size); t_node *add_column(t_node *root); @@ -85,17 +83,17 @@ t_node **add_cols(t_node *root, int number); t_node *add_node(t_node *col); -t_node *add_node_with_coords(t_node *col, t_coord row); +t_node *add_node_with_coord(t_node *col, t_coord row, int type); t_node **add_cols(t_node *root, int number); -void print_solution(t_list *sol); +void print_solution(t_list *sol, t_node *root, int amount); void add_rows(int *types, int amount, int size, t_node **cols_arr); void link_row(int *col_nums, t_node **cols_arr); -void add_row(int *col_numbers, t_node **cols_arr, t_coord coord); +void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type); void search(t_node* root, t_list *solution, int k, int amount); @@ -109,4 +107,6 @@ int get_amount(char **ttr); unsigned int ft_sqrt_ceil(unsigned int num); +void fill_map(char *map, int size, t_node *ttr, int letter); + #endif diff --git a/src/dancing_links.c b/src/dancing_links.c index 2b0b8ec..27271bb 100644 --- a/src/dancing_links.c +++ b/src/dancing_links.c @@ -13,13 +13,14 @@ #include "../inc/fillit.h" #include -t_node *init_root(void) +t_node *init_root(int size) { t_node *new; new = (t_node*)malloc(sizeof(t_node)); new->left = new->right = new->down = new->up = new; new->column = NULL; + new->size = size; return (new); } @@ -38,12 +39,13 @@ t_node *add_column(t_node *root) return (new_col); } -t_node *add_node_with_coord(t_node *col, t_coord coord) +t_node *add_node_with_coord(t_node *col, t_coord coord, int type) { t_node *new_node; new_node = (t_node*)malloc(sizeof(t_node)); new_node->coord = coord; + new_node->type = type; new_node->column = col; new_node->up = col->up; new_node->down = col; @@ -91,9 +93,9 @@ int *get_coords(t_coord coord, int amount, int type, int size) { } return (result); } -void add_row(int *col_numbers, t_node **cols_arr, t_coord coord) +void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type) { - add_node_with_coord(cols_arr[col_numbers[0]], coord); + add_node_with_coord(cols_arr[col_numbers[0]], coord, type); add_node(cols_arr[col_numbers[1]]); add_node(cols_arr[col_numbers[2]]); add_node(cols_arr[col_numbers[3]]); @@ -134,7 +136,7 @@ void add_rows(int *types, int amount, int size, t_node **cols_arr) coord.y = l; col_num = get_coords(coord, amount, types[i], size); col_num[0] = i; - add_row(col_num, cols_arr, coord); + add_row(col_num, cols_arr, coord, types[i]); link_row(col_num, cols_arr); k++; } @@ -185,10 +187,48 @@ void uncover(t_node *to_uncover) to_uncover->right->left = to_uncover; } -void print_solution(t_list *sol) +void fill_map(char *map, int size, t_node *ttr, int letter) { - if (sol) - printf("as\n"); + int i; + char a; + + a = 'A' + letter - 1; + + i = 0; + while (i < 4) + { + ft_memcpy((map + (ttr->coord.y + g_templates[ttr->type].c[i].y) * size + + ttr->coord.x + g_templates[ttr->type].c[i].x), &a, 1); + i++; + } +} + +void print_solution(t_list *sol, t_node *root, int amount) +{ + char *map; + int i; + int map_size; + + map_size = root->size * root->size + 1; + map = malloc(sizeof(char) * map_size); + map[map_size] = 0; + while (sol) + { + fill_map(map, root->size, (*(t_node **)sol->content), amount); + sol = sol->next; + amount--; + } + i = 0; + while (i < root->size * root->size) + { + if (map[i] >= 'A' && map[i] <= 'Z') + ft_putchar(map[i]); + else + ft_putchar('.'); + if ((i + 1) % root->size == 0) + ft_putchar('\n'); + i++; + } exit(0); } @@ -199,7 +239,7 @@ void search(t_node* root, t_list *solution, int k, int amount) t_node *j; if (k == amount) - print_solution(solution); + print_solution(solution, root, amount); if (root->right == root) return ; current_col = root->right; diff --git a/src/main.c b/src/main.c index dd8cf1f..76e9024 100644 --- a/src/main.c +++ b/src/main.c @@ -35,7 +35,7 @@ t_ttrmn g_templates[19] = {"##..##..........", 2, 2, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}}, {"##...##.........", 3, 2, {{0, 0}, {0, 1}, {1, 1}, {1, 2}}}, {".#..##..#.......", 2, 3, {{0, 1}, {1, 0}, {1, 1}, {2, 0}}}, - {".##.##..........", 3, 2, {{0, 1}, {0, 2}, {1, 0}, {1, 2}}}, + {".##.##..........", 3, 2, {{0, 1}, {0, 2}, {1, 0}, {1, 1}}}, {"#...##...#......", 2, 3, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}} }; @@ -76,8 +76,8 @@ int main(int argc, char **argv) //printf("move to up left:\n\n\n"); //print_one_string(ttr); - printf("test check:\n\n"); - test_check(ttr, g_templates); + //printf("test check:\n\n"); + //test_check(ttr, g_templates); @@ -85,7 +85,7 @@ int main(int argc, char **argv) amount = get_amount(ttr); size = ft_sqrt_ceil(amount * 4); while (1) { - r = init_root(); + r = init_root(size); cols_arr = add_cols(r, (size * size + amount)); add_rows(types, amount, size, cols_arr); search(r, ans, 0, amount); diff --git a/tests/color b/tests/color new file mode 100755 index 0000000..4ea5ef2 Binary files /dev/null and b/tests/color differ diff --git a/tests/sample2.fillit b/tests/sample2.fillit new file mode 100644 index 0000000..fa06f41 --- /dev/null +++ b/tests/sample2.fillit @@ -0,0 +1,34 @@ +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... diff --git a/tests/sample3.fillit b/tests/sample3.fillit new file mode 100644 index 0000000..032b064 --- /dev/null +++ b/tests/sample3.fillit @@ -0,0 +1,19 @@ +.... +##.. +.#.. +.#.. + +.... +#### +.... +.... + +#... +###. +.... +.... + +.... +##.. +.##. +.... \ No newline at end of file diff --git a/tests/sample4.fillit b/tests/sample4.fillit new file mode 100644 index 0000000..d275539 --- /dev/null +++ b/tests/sample4.fillit @@ -0,0 +1,39 @@ +...# +...# +...# +...# + +.... +.... +.... +#### + +.### +...# +.... +.... + +.... +..## +.##. +.... + +.... +.##. +.##. +.... + +.... +.... +##.. +.##. + +##.. +.#.. +.#.. +.... + +.... +###. +.#.. +.... \ No newline at end of file diff --git a/tests/sample5.fillit b/tests/sample5.fillit new file mode 100644 index 0000000..d24b25d --- /dev/null +++ b/tests/sample5.fillit @@ -0,0 +1,4 @@ +.... +..## +.##. +.... \ No newline at end of file diff --git a/tests/sample6.fillit b/tests/sample6.fillit new file mode 100644 index 0000000..d7cc980 --- /dev/null +++ b/tests/sample6.fillit @@ -0,0 +1,119 @@ +...# +...# +...# +...# + +.... +.... +.... +#### + +.### +...# +.... +.... + +.... +..## +.##. +.... + +.... +.##. +.##. +.... + +.... +.... +##.. +.##. + +##.. +.#.. +.#.. +.... + +.... +###. +.#.. +.... + +...# +...# +...# +...# + +.... +.... +.... +#### + +.### +...# +.... +.... + +.... +..## +.##. +.... + +.... +.##. +.##. +.... + +.... +.... +##.. +.##. + +##.. +.#.. +.#.. +.... + +.... +###. +.#.. +.... + +...# +...# +...# +...# + +.... +.... +.... +#### + +.### +...# +.... +.... + +.... +..## +.##. +.... + +.... +.##. +.##. +.... + +.... +.... +##.. +.##. + +##.. +.#.. +.#.. +.... + +.... +###. +.#.. +.... \ No newline at end of file