From 28c1cd98fde48f4d888cae70b7e5e6162ba44975 Mon Sep 17 00:00:00 2001 From: Gregory Tertyshny Date: Wed, 21 Dec 2016 20:51:29 +0200 Subject: [PATCH] algorythm that works, maybe --- .gitignore | 3 +++ CMakeLists.txt | 6 +++-- inc/fillit.h | 3 +-- src/dancing_links.c | 52 +++++++++++++++++++++++++------------------- src/main.c | 11 ++++++---- tests/sample1.fillit | 10 ++++----- 6 files changed, 50 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index b8cecc6..ed17cff 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,6 @@ # Sublime files *.sublime* + +# Clion files +.idea diff --git a/CMakeLists.txt b/CMakeLists.txt index bd7a5ca..41f9715 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,13 @@ -cmake_minimum_required(VERSION 3.6) +cmake_minimum_required(VERSION 3.5.2) project(fillit) set(CMAKE_CXX_STANDARD 11) include_directories(inc) +include_directories(libft) set(SOURCE_FILES inc/fillit.h - inc/libft.h libft/ft_atoi.c libft/ft_bzero.c libft/ft_isalnum.c @@ -71,6 +71,7 @@ set(SOURCE_FILES libft/ft_strtrim.c libft/ft_tolower.c libft/ft_toupper.c + libft/ft_lstpop.c libft/libft.h src/check_raw_string.c src/dancing_links.c @@ -85,6 +86,7 @@ set(SOURCE_FILES src/test!_print_one_string.c src/test!_print_table.c src/to_letters.c + src/get_amount.c tests/sample.fillit tests/sample1.fillit) diff --git a/inc/fillit.h b/inc/fillit.h index 81af720..0436fa2 100644 --- a/inc/fillit.h +++ b/inc/fillit.h @@ -25,8 +25,8 @@ typedef struct s_coord { - int x; int y; + int x; } t_coord; typedef struct s_ttrmn @@ -43,7 +43,6 @@ typedef struct s_node struct s_node *right; struct s_node *up; struct s_node *down; - int row; struct s_node *column; t_coord coord; } t_node; diff --git a/src/dancing_links.c b/src/dancing_links.c index 2514ec6..2b0b8ec 100644 --- a/src/dancing_links.c +++ b/src/dancing_links.c @@ -18,7 +18,7 @@ t_node *init_root(void) t_node *new; new = (t_node*)malloc(sizeof(t_node)); - new->left = new->right = new->down = new->up = NULL; + new->left = new->right = new->down = new->up = new; new->column = NULL; return (new); } @@ -32,7 +32,8 @@ t_node *add_column(t_node *root) new_col->up = new_col; new_col->left = root->left; new_col->right = root; - new_col->row = -1; + root->left->right = new_col; + root->left = new_col; new_col->column = NULL; return (new_col); } @@ -43,6 +44,7 @@ t_node *add_node_with_coord(t_node *col, t_coord coord) new_node = (t_node*)malloc(sizeof(t_node)); new_node->coord = coord; + new_node->column = col; new_node->up = col->up; new_node->down = col; col->up->down = new_node; @@ -55,6 +57,7 @@ t_node *add_node(t_node *col) t_node *new_node; new_node = (t_node*)malloc(sizeof(t_node)); + new_node->column = col; new_node->up = col->up; new_node->down = col; col->up->down = new_node; @@ -65,23 +68,27 @@ t_node *add_node(t_node *col) t_node **add_cols(t_node *root, int number) { t_node **array; + int i; array = (t_node**)malloc(number * sizeof(t_node*)); - while (number--) - array[number] = add_column(root); + i = 0; + while (i < number) + array[i++] = add_column(root); return (array); } -int *get_coords(t_coord coord, int amount, int type, int size) -{ +int *get_coords(t_coord coord, int amount, int type, int size) { int *result; int i; t_coord *start; - result = (int*)malloc(sizeof(int) * 4); + result = (int *) malloc(sizeof(int) * 5); start = g_templates[type].c; - i = 1; - while (i < 5) - result[i++] = coord.x + coord.y * size + start->x + start->y * size + amount; + i = 0; + while (i < 4) + { + result[i + 1] = (coord.x + coord.y * size + start[i].x + start[i].y * size) + amount; + i++; + } return (result); } void add_row(int *col_numbers, t_node **cols_arr, t_coord coord) @@ -117,11 +124,11 @@ void add_rows(int *types, int amount, int size, t_node **cols_arr) while (i < amount) { - k = 0; - while (k < size - g_templates[types[i]].width) + l = 0; + while (l <= size - g_templates[types[i]].height) { - l = 0; - while (l < size - g_templates[types[i]].height) + k = 0; + while (k <= size - g_templates[types[i]].width) { coord.x = k; coord.y = l; @@ -129,9 +136,9 @@ void add_rows(int *types, int amount, int size, t_node **cols_arr) col_num[0] = i; add_row(col_num, cols_arr, coord); link_row(col_num, cols_arr); - l++; + k++; } - k++; + l++; } i++; } @@ -180,8 +187,9 @@ void uncover(t_node *to_uncover) void print_solution(t_list *sol) { - sol = 0; - printf("as\n"); + if (sol) + printf("as\n"); + exit(0); } void search(t_node* root, t_list *solution, int k, int amount) @@ -199,20 +207,20 @@ void search(t_node* root, t_list *solution, int k, int amount) row = current_col->down; while (row != current_col) { - ft_lstadd(&solution, ft_lstnew(row, sizeof(row))); + ft_lstadd(&solution, ft_lstnew(&row, sizeof(t_node *))); j = row->right; while (j != row) { - cover(j); + cover(j->column); j = j->right; } search(root, solution, k + 1, amount); - row = (t_node*)ft_lstpop(&solution); + row = *((t_node**)(ft_lstpop(&solution)->content)); current_col = row->column; j = row->left; while (j != row) { - uncover(j); + uncover(j->column); j = j->left; } row = row->down; diff --git a/src/main.c b/src/main.c index 1640555..dd8cf1f 100644 --- a/src/main.c +++ b/src/main.c @@ -84,10 +84,13 @@ int main(int argc, char **argv) types = get_types(ttr); amount = get_amount(ttr); size = ft_sqrt_ceil(amount * 4); - r = init_root(); - cols_arr = add_cols(r, (size * size + amount)); - add_rows(types, amount, size, cols_arr); - //search(r, ans, 0, amount); + while (1) { + r = init_root(); + cols_arr = add_cols(r, (size * size + amount)); + add_rows(types, amount, size, cols_arr); + search(r, ans, 0, amount); + size++; + } //print_ans(ans); //printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y); //to_letters(ttr); diff --git a/tests/sample1.fillit b/tests/sample1.fillit index 5b65dda..cfb9f31 100644 --- a/tests/sample1.fillit +++ b/tests/sample1.fillit @@ -1,8 +1,3 @@ -#### -.... -.... -.... - .... ##.. .#.. @@ -17,3 +12,8 @@ ##.. ##.. .... + +#### +.... +.... +....