From 67cb5dea38578c9c8f2bd226bffac36d8577ad2f Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Thu, 9 May 2019 18:04:08 +0200 Subject: [PATCH 1/8] en cours decriture de check_others --- search_map.c | 62 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/search_map.c b/search_map.c index b1176eb..3805c1a 100644 --- a/search_map.c +++ b/search_map.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/08 18:38:47 by hulamy ### ########.fr */ +/* Updated: 2019/05/09 18:03:46 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,11 +79,50 @@ void add_remove(unsigned int *map, t_fillist *list, int size, int pos) } } +int check_others(unsigned int *map, t_fillist *list, int size, int num) +{ + t_fillist *tmp; + int dots; + int total; + int i; + int j; + + dot = 0; + i = -1; + num *= 4; + total = size * size; + while (++i < total && dots <= total - num) + { + tmp = list->next; + j = 1; + while (verifier si i est un point) + i++; + while (j && tmp) + { + // si le tetri est trop a droite ou trop en bas il ne rentre pas ce n'est pas la peine de charcher + if (tmp->width > (size - i % size) || (total - i) <= (tmp->height * size)) + tmp = tmp->next; + else if (tmp->tetribit ne rentre pas dans la place i) + tmp = tmp->next; + else + j = 0; + } + if (j) + dots++; + } + // verifie que les tetri restant puissent un par un se placer sur la map + // ca n'optimise qu'en fin de map donc ca ralentit les grosses map en fait +// while ((tmp = tmp->next)) +// if (!find_place(map, tmp, size, 0)) +// return (0); + return (1); +} + /* ** function that recursively try to fill the map with the tetris */ -int fill_map(unsigned int *map, t_fillist *list, int size, t_fillist *link) // DEBUG "link" +int fill_map(unsigned int *map, t_fillist *list, int size, int num, t_fillist *link) // DEBUG "link" { int pos; @@ -95,14 +134,11 @@ int fill_map(unsigned int *map, t_fillist *list, int size, t_fillist *link) // { add_remove(map, list, size, pos); list->position = pos; -// print_final_map(link, size); // DEBUG -// ft_putnbrendl(pos); -// print_map(map, size, size, '#'); // DEBUG -// ft_putchar('\n'); - if (fill_map(map, list->next, size, link)) + print_final_map(link, size); ft_putnbrendl(pos); print_map(map, size, size, '#'); ft_putchar('\n'); // DEBUG + if (check_others(map, list, size, num) && fill_map(map, list->next, size, num, link)) return (1); add_remove(map, list, size, pos); -// list->position = -1; // DEBUG + list->position = -1; // DEBUG } return (0); } @@ -133,16 +169,16 @@ void search_map(t_fillist *list) unsigned int *map; int size; - int i; + int num; size = 2; - i = 1; + num = 1; tmp = list; // trouve le nombre de tetri en parcourant la liste chainee while ((tmp = tmp->next)) - i++; + num++; // trouve la taille minimale de la map - while (size * size < i * 4) + while (size * size < num * 4) size++; map = init_map(size); @@ -162,7 +198,7 @@ void search_map(t_fillist *list) // lance la recursive fill_map en augmentant la taille de la map tant qu'il n'y a pas de solution - while (!fill_map(map, list, size, list)) + while (!fill_map(map, list, size, num, list)) map = init_map(size++); print_final_map(list, size); // DEBUG print_map(map, size, size, '#'); // DEBUG From 213eff32557eecdd5b643d3c0567500755b99c8e Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Fri, 10 May 2019 17:00:55 +0200 Subject: [PATCH 2/8] rate pour l'opti qui ralenti mais find_place divisee avec fit_in_place --- .search_map.c.swn | Bin 0 -> 16384 bytes search_map.c | 88 +++++++++++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 .search_map.c.swn diff --git a/.search_map.c.swn b/.search_map.c.swn new file mode 100644 index 0000000000000000000000000000000000000000..0dee589a44d9a36b7a9fac698715f4b83b33cbd6 GIT binary patch literal 16384 zcmeI3ON<;x8ONId!4sSq*#aRBQQ0fJ^YD81g&mK*D+qZg5-18Hkx=ZcdS`k(wcFD@ z?#HejI}gZ95S%Cq2gE}n2@*m9aUhC(KnMlJBya%{@rnfD0*FJz4H0qR_f>WG^sd*& zm>fXrw*K3Xs`|R>UtfJS^{@8w^j)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_s5NG`mplvbkwPRWEllhsa;n_UP8a)6^bc2djIWe*t1)b-@4t literal 0 HcmV?d00001 diff --git a/search_map.c b/search_map.c index 3805c1a..b69c5dd 100644 --- a/search_map.c +++ b/search_map.c @@ -6,49 +6,52 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/09 18:03:46 by hulamy ### ########.fr */ +/* Updated: 2019/05/10 17:00:08 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "fillit.h" +/* +** function that look if a tretri fit in a place +*/ + +unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos) +{ + unsigned int tmp; + unsigned int mask; + int j; + + mask = ~0u << (32 - list->width); // creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000) + j = (list->height - 1) * size + pos; + tmp = 0; + while (j >= pos) + { + tmp >>= list->width; + tmp |= (mask & (map[j / 32] << j)); + tmp |= (mask & (map[(j + list->width) / 32] >> (32 - j))); + j -= size; + } + if (!((tmp >> 16) & list->tetribit)) + return (pos + 1); + return (0); +} + /* ** function that look for the first place in the map for a tetri */ int find_place(unsigned int *tab, t_fillist *list, int size, int pos) { - int i; - int j; - unsigned int mask; - unsigned int tmp; - - // creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000) - mask = ~0u << (32 - list->width); - tmp = mask; - i = pos; // boucle jusqu'a la dernier place pour le tetri dans la map ou qu'il fit dans un trou - while (i < (size - list->height + 1) * size) + while (pos < (size - list->height + 1) * size) { // pour ne pas deborder a droite de la map - if (i % size == size - list->width + 1) - i += list->width - 1; - else - { - tmp = 0; - // construit un tmp qui est une photo de la map de la taille du tetri a un emplacement donne - j = (list->height - 1) * size + i; - while (j >= i) - { - tmp >>= list->width; - tmp |= (mask & (tab[j / 32] << j)); - tmp |= (mask & (tab[(j + list->width) / 32] >> (32 - j))); - j -= size; - } - if (!((tmp >> 16) & list->tetribit)) - return (i + 1); - i++; - } + if (pos % size == size - list->width + 1) + pos += list->width - 2; + else if (fit_in_place(tab, list, size, pos)) + return (pos + 1); + pos++; } return (0); } @@ -79,6 +82,11 @@ void add_remove(unsigned int *map, t_fillist *list, int size, int pos) } } +/* +** function that try to optimize the speed +** TOTAL FAILURE :p +*/ + int check_others(unsigned int *map, t_fillist *list, int size, int num) { t_fillist *tmp; @@ -87,7 +95,7 @@ int check_others(unsigned int *map, t_fillist *list, int size, int num) int i; int j; - dot = 0; + dots = 0; i = -1; num *= 4; total = size * size; @@ -95,27 +103,27 @@ int check_others(unsigned int *map, t_fillist *list, int size, int num) { tmp = list->next; j = 1; - while (verifier si i est un point) + // saute les position pas vides + while (1 << (i % 32) & map[i % 32]) i++; + // pour chaque position vide regarde si chaque tetri non encode places peuvent y rentrer while (j && tmp) { - // si le tetri est trop a droite ou trop en bas il ne rentre pas ce n'est pas la peine de charcher + // si le tetri est trop a droite ou trop en bas il ne rentre pas ce n'est pas la peine de chercher donc on passe au tetri suivant if (tmp->width > (size - i % size) || (total - i) <= (tmp->height * size)) tmp = tmp->next; - else if (tmp->tetribit ne rentre pas dans la place i) + // sinon verifier si on peut le mettre a cette position et si on ne peut pas passer au tetri suivant + else if (!fit_in_place(map, list, size, i)) tmp = tmp->next; + // si le tetri peut rentrer on arrete la boucle en mettant j = 0 else j = 0; } + // si j existe c que le tetri ne pouvait pas etre place donc on rajoute 1 au compteur de point isoles if (j) dots++; } - // verifie que les tetri restant puissent un par un se placer sur la map - // ca n'optimise qu'en fin de map donc ca ralentit les grosses map en fait -// while ((tmp = tmp->next)) -// if (!find_place(map, tmp, size, 0)) -// return (0); - return (1); + return (dots > total - num); } /* @@ -134,7 +142,7 @@ int fill_map(unsigned int *map, t_fillist *list, int size, int num, t_fillist * { add_remove(map, list, size, pos); list->position = pos; - print_final_map(link, size); ft_putnbrendl(pos); print_map(map, size, size, '#'); ft_putchar('\n'); // DEBUG +// print_final_map(link, size); ft_putnbrendl(pos); print_map(map, size, size, '#'); ft_putchar('\n'); // DEBUG if (check_others(map, list, size, num) && fill_map(map, list->next, size, num, link)) return (1); add_remove(map, list, size, pos); From aa8ec6fe66e3eff2273593bcec75412845734e43 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Fri, 10 May 2019 20:40:16 +0200 Subject: [PATCH 3/8] tentative echouee d'oter les modulo et division dans le fit_in_place --- .search_map.c.swn | Bin 16384 -> 0 bytes search_map.c | 116 ++++++++++++++++++++-------------------------- 2 files changed, 50 insertions(+), 66 deletions(-) delete mode 100644 .search_map.c.swn diff --git a/.search_map.c.swn b/.search_map.c.swn deleted file mode 100644 index 0dee589a44d9a36b7a9fac698715f4b83b33cbd6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI3ON<;x8ONId!4sSq*#aRBQQ0fJ^YD81g&mK*D+qZg5-18Hkx=ZcdS`k(wcFD@ z?#HejI}gZ95S%Cq2gE}n2@*m9aUhC(KnMlJBya%{@rnfD0*FJz4H0qR_f>WG^sd*& zm>fXrw*K3Xs`|R>UtfJS^{@8w^j)`oQXSks?{K`&ah7j??dbCB*Em1?gyRfS-yNQk zgi9aZTr@X2uz!Abe?PiZ&ik#wc0Wv`U`)YrbRlXmR6QFpL3bVvKUm$Ff% zRSZ-NTqy(BI&-r#(^7h3&u+DA=bcw-s47=6P%%(3P%%(3P%%(3P%%(3P%-d-$Uri> z%6Wi#UzOGUOm=?9rt>?q`#ZApeVfv=+v-#?P%%(3P%%(3P%%(3P%%(3P%%(3P%%(3 zP%-d7$bj2(oGI#cNCW`(|MT_#_jWkWJ>XjKgXR=MQ@m|OIJ@_Sf1-t;B0p9?(fC=ycupPW`jpIBD9s+lPeP9>Z4z_`R)*R<+ zU=8%Z61W9i5B{*taefYd2A%^?g2%u^;6V_B6JP{};4ruW90Y%RkK?=veg}RG&VlcO z?|`Sl2;2rHz@M&moY%lFz|-JCa0u)J9|Z3QyTDHH>bo81C*Vc!ZSYm_6nG3g3eJ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jAj2Y#8qK#Z1BcYoxE8`Z1X-ECv;zs*w4KeOJ#B+txj&KWHWE_ z#{8FYC)UOmsb^+AH(35~{4&f*mQJfQ+Ydf<3Z?Ud@oOQ6Q|MUI*uk2u537!CF!9!pjXz&&~z$@To@C^7o5N+TkG=cAf zCqW2&ump~Qqu?`OJE(ze;IHTde*v$7^WbIhJXi-B+y!n0H-nqN$H4381it~l1}}(3&=>SLtMpISuR^O8)JhIc6`5}Ei zM9d${BUGCrMitKNU1X~uw}to|4wsS8S+H6)i*82I?;>qv-7F0=YBahuTH%J)O8u;? zZ0fzBW=kR-iL@(QwoCeEdPK67{KfjbR*1bu#T&1D>O~f->oy#RpY|8*&afrXlPwvu z^cJVxgA)q#VV{ex7vZw(R0zc`Ia#SA<%wJ*a;bTovN6JTj!q_d5c+FgZ?mQsc@?VZYuKW4Y>s1ycnIX`Zp~AbPXNO(jkx z#|1QEMU;>YuGcK$0UaRQAzKnAnF=KX;zZ|&%pQlSirLySTUuhoJCU+0K(2c|HjOs( zQfQr4)5o&F{^H^)|ieOu6 xUwN0)D%z5lv0$sr7_s5NG`mplvbkwPRWEllhsa;n_UP8a)6^bc2djIWe*t1)b-@4t diff --git a/search_map.c b/search_map.c index b69c5dd..262131a 100644 --- a/search_map.c +++ b/search_map.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/10 17:00:08 by hulamy ### ########.fr */ +/* Updated: 2019/05/10 20:39:19 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,25 +16,42 @@ ** function that look if a tretri fit in a place */ -unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos) +unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos, int number, int place) { unsigned int tmp; unsigned int mask; - int j; + int i; - mask = ~0u << (32 - list->width); // creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000) - j = (list->height - 1) * size + pos; +// (void)number; +// (void)place; + (void)pos; + + i = -1; tmp = 0; - while (j >= pos) + mask = ~0u << (32 - list->width); // creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000) + while (++i < list->height) { - tmp >>= list->width; - tmp |= (mask & (map[j / 32] << j)); - tmp |= (mask & (map[(j + list->width) / 32] >> (32 - j))); - j -= size; + if (place >= 32) + { + place -= 32; + number++; + } + tmp <<= list->width; + tmp |= ((mask & (map[number] << place)) | (mask & (map[number + 1] >> (32 - place)))) >> (32 - list->width); + place += size; } - if (!((tmp >> 16) & list->tetribit)) - return (pos + 1); - return (0); + +// mask = ~0u << (32 - list->width); +// i = (list->height - 1) * size + pos; +// tmp = 0; +// while (i >= pos) +// { +// tmp >>= list->width; +// tmp |= (mask & (map[i / 32] << i)) | (mask & (map[(i / 32) + 1] >> (32 - i))); +// i -= size; +// } + + return (!((tmp >> 16) & list->tetribit)); } /* @@ -43,15 +60,27 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos) int find_place(unsigned int *tab, t_fillist *list, int size, int pos) { - // boucle jusqu'a la dernier place pour le tetri dans la map ou qu'il fit dans un trou + int row; + int number; + int place; + + row = pos % size; + place = pos % 32; + number = pos / 32; + // boucle jusqu'a la dernier place pour le tetri dans la map ou qu'il entre dans un trou while (pos < (size - list->height + 1) * size) { // pour ne pas deborder a droite de la map - if (pos % size == size - list->width + 1) +// if (pos % size > size - list->width) + if (place >= 32 && ++number) + place -= 32; + if (row > size - list->width && (row = -1)) pos += list->width - 2; - else if (fit_in_place(tab, list, size, pos)) + else if (fit_in_place(tab, list, size, pos, number, place)) return (pos + 1); pos++; + row++; + place++; } return (0); } @@ -75,62 +104,17 @@ void add_remove(unsigned int *map, t_fillist *list, int size, int pos) while (j >= pos) { map[(j - 1) / 32] ^= (mask & tetri << (16 + i)) >> (j - 1); - if (j % 32 != 1) - map[(j + size) / 32] ^= (mask & tetri << (16 + i)) << (32 - j + 1); + map[(j - 1) / 32 + 1] ^= (mask & tetri << (16 + i)) << (32 - j) << 1; j -= size; i -= list->width; } } -/* -** function that try to optimize the speed -** TOTAL FAILURE :p -*/ - -int check_others(unsigned int *map, t_fillist *list, int size, int num) -{ - t_fillist *tmp; - int dots; - int total; - int i; - int j; - - dots = 0; - i = -1; - num *= 4; - total = size * size; - while (++i < total && dots <= total - num) - { - tmp = list->next; - j = 1; - // saute les position pas vides - while (1 << (i % 32) & map[i % 32]) - i++; - // pour chaque position vide regarde si chaque tetri non encode places peuvent y rentrer - while (j && tmp) - { - // si le tetri est trop a droite ou trop en bas il ne rentre pas ce n'est pas la peine de chercher donc on passe au tetri suivant - if (tmp->width > (size - i % size) || (total - i) <= (tmp->height * size)) - tmp = tmp->next; - // sinon verifier si on peut le mettre a cette position et si on ne peut pas passer au tetri suivant - else if (!fit_in_place(map, list, size, i)) - tmp = tmp->next; - // si le tetri peut rentrer on arrete la boucle en mettant j = 0 - else - j = 0; - } - // si j existe c que le tetri ne pouvait pas etre place donc on rajoute 1 au compteur de point isoles - if (j) - dots++; - } - return (dots > total - num); -} - /* ** function that recursively try to fill the map with the tetris */ -int fill_map(unsigned int *map, t_fillist *list, int size, int num, t_fillist *link) // DEBUG "link" +int fill_map(unsigned int *map, t_fillist *list, int size, t_fillist *link) // DEBUG "link" { int pos; @@ -143,10 +127,10 @@ int fill_map(unsigned int *map, t_fillist *list, int size, int num, t_fillist * add_remove(map, list, size, pos); list->position = pos; // print_final_map(link, size); ft_putnbrendl(pos); print_map(map, size, size, '#'); ft_putchar('\n'); // DEBUG - if (check_others(map, list, size, num) && fill_map(map, list->next, size, num, link)) + if (fill_map(map, list->next, size, link)) return (1); add_remove(map, list, size, pos); - list->position = -1; // DEBUG +// list->position = -1; // DEBUG } return (0); } @@ -206,7 +190,7 @@ void search_map(t_fillist *list) // lance la recursive fill_map en augmentant la taille de la map tant qu'il n'y a pas de solution - while (!fill_map(map, list, size, num, list)) + while (!fill_map(map, list, size, list)) map = init_map(size++); print_final_map(list, size); // DEBUG print_map(map, size, size, '#'); // DEBUG From af527c63169e9d7dc43daf93a205bd2afbdf8873 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Fri, 10 May 2019 20:46:02 +0200 Subject: [PATCH 4/8] fonctionne mais sans optimisation --- search_map.c | 52 ++++++++++------------------------------------------ 1 file changed, 10 insertions(+), 42 deletions(-) diff --git a/search_map.c b/search_map.c index 262131a..a0c287f 100644 --- a/search_map.c +++ b/search_map.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/10 20:39:19 by hulamy ### ########.fr */ +/* Updated: 2019/05/10 20:45:35 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,41 +16,21 @@ ** function that look if a tretri fit in a place */ -unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos, int number, int place) +unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos) { unsigned int tmp; unsigned int mask; int i; -// (void)number; -// (void)place; - (void)pos; - - i = -1; + mask = ~0u << (32 - list->width); + i = (list->height - 1) * size + pos; tmp = 0; - mask = ~0u << (32 - list->width); // creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000) - while (++i < list->height) + while (i >= pos) { - if (place >= 32) - { - place -= 32; - number++; - } - tmp <<= list->width; - tmp |= ((mask & (map[number] << place)) | (mask & (map[number + 1] >> (32 - place)))) >> (32 - list->width); - place += size; + tmp >>= list->width; + tmp |= (mask & (map[i / 32] << i)) | (mask & (map[(i / 32) + 1] >> (32 - i))); + i -= size; } - -// mask = ~0u << (32 - list->width); -// i = (list->height - 1) * size + pos; -// tmp = 0; -// while (i >= pos) -// { -// tmp >>= list->width; -// tmp |= (mask & (map[i / 32] << i)) | (mask & (map[(i / 32) + 1] >> (32 - i))); -// i -= size; -// } - return (!((tmp >> 16) & list->tetribit)); } @@ -60,27 +40,15 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos, int find_place(unsigned int *tab, t_fillist *list, int size, int pos) { - int row; - int number; - int place; - - row = pos % size; - place = pos % 32; - number = pos / 32; // boucle jusqu'a la dernier place pour le tetri dans la map ou qu'il entre dans un trou while (pos < (size - list->height + 1) * size) { // pour ne pas deborder a droite de la map -// if (pos % size > size - list->width) - if (place >= 32 && ++number) - place -= 32; - if (row > size - list->width && (row = -1)) + if (pos % size > size - list->width) pos += list->width - 2; - else if (fit_in_place(tab, list, size, pos, number, place)) + else if (fit_in_place(tab, list, size, pos)) return (pos + 1); pos++; - row++; - place++; } return (0); } From 2e53b04a9abababb70643fafc3d76c1fc2d054f9 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Wed, 15 May 2019 17:47:32 +0200 Subject: [PATCH 5/8] optimisation en virant modulos et divisions semble marcher, gagne 10s --- search_map.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/search_map.c b/search_map.c index a0c287f..daf835a 100644 --- a/search_map.c +++ b/search_map.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/10 20:45:35 by hulamy ### ########.fr */ +/* Updated: 2019/05/15 17:46:55 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,36 @@ ** function that look if a tretri fit in a place */ -unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos) +//unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos) +unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos, int number, int place) { unsigned int tmp; unsigned int mask; int i; +// /* + (void)pos; + unsigned int tetri; + i = list->height; + tetri = list->tetribit << 16 >> list->width; + tmp = 0; + mask = ~0u << (32 - list->width); + while (i--) + { + if (tmp & tetri) + return (0); + if (place >= 32 && ++number) + place -= 32; + tmp = (mask & (map[number] << place)) | (mask & (map[number + 1] >> (32 - place))); + tetri <<= list->width; + place += size; + } + return (!(tmp & tetri)); +// */ + + /* + (void)place; + (void)number; mask = ~0u << (32 - list->width); i = (list->height - 1) * size + pos; tmp = 0; @@ -32,6 +56,7 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos) i -= size; } return (!((tmp >> 16) & list->tetribit)); + */ } /* @@ -40,6 +65,36 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos) int find_place(unsigned int *tab, t_fillist *list, int size, int pos) { +// /* + int row; // designe la position de size*size sur la ligne du tableau + int number; // designe la position de pos dans l'int du tableau + int place; // designe dans quel int tu tableau pos se trouve + int limit; // limit en hauteur du tableau a chercher pour la position du tetri + + row = pos % size; + place = pos % 32; + number = pos / 32; + limit = (size - list->height + 1) * size; + while (pos < limit) + { + if (place >= 32 && ++number) + place -= 32; +// if (place != (pos % 32) || number != (pos / 32)) // pour verifier que place et number sont bien implementes +// ft_putendl("error"); + if (row > size - list->width && (row = -1)) + { + pos += list->width - 2; + place += list->width - 2; + } + else if (fit_in_place(tab, list, size, pos, number, place)) + return (pos + 1); + pos++; + row++; + place++; + } +// */ + + /* // boucle jusqu'a la dernier place pour le tetri dans la map ou qu'il entre dans un trou while (pos < (size - list->height + 1) * size) { @@ -50,6 +105,8 @@ int find_place(unsigned int *tab, t_fillist *list, int size, int pos) return (pos + 1); pos++; } + */ + return (0); } From 6705bf9133d72e2a4682ff4bc3e2cffb9300532c Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Wed, 15 May 2019 18:05:37 +0200 Subject: [PATCH 6/8] version clean sans modulos et divisions pour find_place --- search_map.c | 73 +++++++++++++--------------------------------------- 1 file changed, 18 insertions(+), 55 deletions(-) diff --git a/search_map.c b/search_map.c index daf835a..9574347 100644 --- a/search_map.c +++ b/search_map.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/15 17:46:55 by hulamy ### ########.fr */ +/* Updated: 2019/05/15 18:03:41 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,15 +16,12 @@ ** function that look if a tretri fit in a place */ -//unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos) -unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos, int number, int place) +unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int n, int r) { unsigned int tmp; unsigned int mask; int i; -// /* - (void)pos; unsigned int tetri; i = list->height; tetri = list->tetribit << 16 >> list->width; @@ -34,29 +31,13 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos, { if (tmp & tetri) return (0); - if (place >= 32 && ++number) - place -= 32; - tmp = (mask & (map[number] << place)) | (mask & (map[number + 1] >> (32 - place))); + if (r >= 32 && ++n) + r -= 32; + tmp = (mask & (map[n] << r)) | (mask & (map[n + 1] >> (32 - r))); tetri <<= list->width; - place += size; + r += size; } return (!(tmp & tetri)); -// */ - - /* - (void)place; - (void)number; - mask = ~0u << (32 - list->width); - i = (list->height - 1) * size + pos; - tmp = 0; - while (i >= pos) - { - tmp >>= list->width; - tmp |= (mask & (map[i / 32] << i)) | (mask & (map[(i / 32) + 1] >> (32 - i))); - i -= size; - } - return (!((tmp >> 16) & list->tetribit)); - */ } /* @@ -65,48 +46,30 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int pos, int find_place(unsigned int *tab, t_fillist *list, int size, int pos) { -// /* - int row; // designe la position de size*size sur la ligne du tableau - int number; // designe la position de pos dans l'int du tableau - int place; // designe dans quel int tu tableau pos se trouve + int place; // designe la position sur la ligne du tableau de size*size + int r; // designe la position de pos dans l'int du tableau + int n; // designe dans quel int tu tableau pos se trouve int limit; // limit en hauteur du tableau a chercher pour la position du tetri - row = pos % size; - place = pos % 32; - number = pos / 32; + place = pos % size; + r = pos % 32; + n = pos / 32; limit = (size - list->height + 1) * size; while (pos < limit) { - if (place >= 32 && ++number) - place -= 32; -// if (place != (pos % 32) || number != (pos / 32)) // pour verifier que place et number sont bien implementes -// ft_putendl("error"); - if (row > size - list->width && (row = -1)) + if (r >= 32 && ++n) + r -= 32; + if (place > size - list->width && (place = -1)) { pos += list->width - 2; - place += list->width - 2; + r += list->width - 2; } - else if (fit_in_place(tab, list, size, pos, number, place)) + else if (fit_in_place(tab, list, size, n, r)) return (pos + 1); pos++; - row++; place++; + r++; } -// */ - - /* - // boucle jusqu'a la dernier place pour le tetri dans la map ou qu'il entre dans un trou - while (pos < (size - list->height + 1) * size) - { - // pour ne pas deborder a droite de la map - if (pos % size > size - list->width) - pos += list->width - 2; - else if (fit_in_place(tab, list, size, pos)) - return (pos + 1); - pos++; - } - */ - return (0); } From e5aea30023e53e68534af292ad16d2435b9b3555 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Thu, 16 May 2019 16:51:09 +0200 Subject: [PATCH 7/8] backtracking fonctionne avec list->position directement --- fillit.dSYM/Contents/Info.plist | 20 ++++++++++++++ fillit.h | 3 ++- parse_input.c | 5 ++-- print.c | 4 +-- search_map.c | 48 +++++++++++++++++++++++++-------- 5 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 fillit.dSYM/Contents/Info.plist diff --git a/fillit.dSYM/Contents/Info.plist b/fillit.dSYM/Contents/Info.plist new file mode 100644 index 0000000..96bfe2d --- /dev/null +++ b/fillit.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.fillit + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/fillit.h b/fillit.h index 9900eb5..413f2c0 100644 --- a/fillit.h +++ b/fillit.h @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */ -/* Updated: 2019/05/08 17:52:19 by hulamy ### ########.fr */ +/* Updated: 2019/05/16 15:31:15 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,6 +44,7 @@ typedef struct s_fillist int width; int height; int position; + int test; char letter; struct s_fillist *next; } t_fillist; diff --git a/parse_input.c b/parse_input.c index 517215e..7ee2852 100644 --- a/parse_input.c +++ b/parse_input.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */ -/* Updated: 2019/05/06 16:30:45 by hulamy ### ########.fr */ +/* Updated: 2019/05/16 15:30:57 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -89,7 +89,8 @@ void fill_list(char line[], t_fillist *list) list->height = i; // fabrique la ligne pour le tetriminos de la bonne largeur list->tetribit = reduce_tetri(list->tetribit, list->width); - list->position = -1; // DEBUG pour que print_final_map puisse imprimer correctement au fur et a mesure + list->position = 0; + list->test = 0; // DEBUG pour que print_final_map puisse imprimer correctement au fur et a mesure } /* diff --git a/print.c b/print.c index ce77edd..d5b053f 100644 --- a/print.c +++ b/print.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/30 13:24:28 by hulamy #+# #+# */ -/* Updated: 2019/05/08 17:52:14 by hulamy ### ########.fr */ +/* Updated: 2019/05/16 15:52:52 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -107,7 +107,7 @@ void print_final_map(t_fillist *list, int size) { if (i && i % tmp->width == 0) j += size - tmp->width; - if (1 << (15 - i) & tmp->tetribit && tmp->position != -1) // DEBUG "&& tmp->position != -1" pour imprimer les bonnes lettres au coours du debug + if (1 << (15 - i) & tmp->tetribit/* && tmp->test == 1*/) // DEBUG "&& tmp->position != -1" pour imprimer les bonnes lettres au coours du debug map[tmp->position + i + j - 1] = tmp->letter; } tmp = tmp->next; diff --git a/search_map.c b/search_map.c index 9574347..7009f04 100644 --- a/search_map.c +++ b/search_map.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/15 18:03:41 by hulamy ### ########.fr */ +/* Updated: 2019/05/16 16:50:29 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,13 +44,15 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int n, i ** function that look for the first place in the map for a tetri */ -int find_place(unsigned int *tab, t_fillist *list, int size, int pos) +int find_place(unsigned int *tab, t_fillist *list, int size) { int place; // designe la position sur la ligne du tableau de size*size int r; // designe la position de pos dans l'int du tableau int n; // designe dans quel int tu tableau pos se trouve int limit; // limit en hauteur du tableau a chercher pour la position du tetri + int pos; + pos = list->position; place = pos % size; r = pos % 32; n = pos / 32; @@ -65,7 +67,8 @@ int find_place(unsigned int *tab, t_fillist *list, int size, int pos) r += list->width - 2; } else if (fit_in_place(tab, list, size, n, r)) - return (pos + 1); + return ((list->position = pos + 1)); +// return (pos + 1); pos++; place++; r++; @@ -77,7 +80,7 @@ int find_place(unsigned int *tab, t_fillist *list, int size, int pos) ** function that add or remove a tetri on the map */ -void add_remove(unsigned int *map, t_fillist *list, int size, int pos) +void add_remove(unsigned int *map, t_fillist *list, int size) { unsigned int mask; unsigned short tetri; @@ -87,9 +90,9 @@ void add_remove(unsigned int *map, t_fillist *list, int size, int pos) tetri = list->tetribit; mask = ~0u << (32 - list->width); i = (list->height - 1) * list->width; - j = (list->height - 1) * size + pos; + j = (list->height - 1) * size + list->position; // change les bits du tetri sur la map a la position donnee - while (j >= pos) + while (j >= list->position) { map[(j - 1) / 32] ^= (mask & tetri << (16 + i)) >> (j - 1); map[(j - 1) / 32 + 1] ^= (mask & tetri << (16 + i)) << (32 - j) << 1; @@ -102,24 +105,47 @@ void add_remove(unsigned int *map, t_fillist *list, int size, int pos) ** function that recursively try to fill the map with the tetris */ -int fill_map(unsigned int *map, t_fillist *list, int size, t_fillist *link) // DEBUG "link" +int fill_map(unsigned int *map, t_fillist *list, int size, t_fillist *link) // DEBUG link sert uniquement pour afficher le debug { - int pos; +// /* +// int posx; +// int posy; +// posx = list->posx; +// posy = list->posy; + if (!list) + return (1); + list->position = 0; // comme les positions sont marquees en dure dans la liste et non plus dans une petite variable + // au sein de la fonction, il faut remettre a zero la position a chaque nouveau tour de map + while (find_place(map, list, size)) + { +// pos = list->position; +// list->test = 1; // DEBUG pour afficher la map a chaque etape + add_remove(map, list, size); +// print_final_map(link, size); ft_putnbrendl(pos); print_map(map, size, size, '#'); ft_putchar('\n'); // DEBUG + if (fill_map(map, list->next, size, link)) + return (1); + add_remove(map, list, size); +// list->test = 0; // DEBUG pour afficher la map a chaque etape + } +// */ + + /* + int pos; + pos = 0; if (!list) return (1); -// unsigned int tmp = list->tetribit << 16; print_map(&tmp, list->width, list->height, list->letter); // DEBUG while ((pos = find_place(map, list, size, pos))) { add_remove(map, list, size, pos); list->position = pos; -// print_final_map(link, size); ft_putnbrendl(pos); print_map(map, size, size, '#'); ft_putchar('\n'); // DEBUG if (fill_map(map, list->next, size, link)) return (1); add_remove(map, list, size, pos); -// list->position = -1; // DEBUG } + */ + return (0); } From 94c0e5d57ff1726742cf3565ee5828632b596806 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Fri, 17 May 2019 17:13:31 +0200 Subject: [PATCH 8/8] search_map a la norme --- Makefile | 4 +- fillit.dSYM/Contents/Info.plist | 20 ----- fillit.h | 7 +- search_map.c | 146 ++++++++++++-------------------- 4 files changed, 59 insertions(+), 118 deletions(-) delete mode 100644 fillit.dSYM/Contents/Info.plist diff --git a/Makefile b/Makefile index 754fcd1..2e01b52 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: vmanzoni +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/03/01 13:24:35 by vmanzoni #+# #+# # -# Updated: 2019/05/06 16:37:52 by hulamy ### ########.fr # +# Updated: 2019/05/17 17:11:53 by hulamy ### ########.fr # # # # **************************************************************************** # @@ -25,7 +25,7 @@ LDLIBS = -lft SRCS = $(shell find . -depth 1 -type f -not -name '.*' -not -name 'test*' -name '*.c') -TRASH = $(shell find . -depth 1 -type f -name '*.dSYM') +TRASH = $(shell find . -depth 1 -name '*.dSYM') TRASH += $(shell find . -depth 1 -type f -name '*.o') TRASH += $(shell find . -depth 1 -type f -name '*.swp') TRASH += $(shell find . -depth 1 -type f -name '*.swo') diff --git a/fillit.dSYM/Contents/Info.plist b/fillit.dSYM/Contents/Info.plist deleted file mode 100644 index 96bfe2d..0000000 --- a/fillit.dSYM/Contents/Info.plist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - CFBundleDevelopmentRegion - English - CFBundleIdentifier - com.apple.xcode.dsym.fillit - CFBundleInfoDictionaryVersion - 6.0 - CFBundlePackageType - dSYM - CFBundleSignature - ???? - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1 - - diff --git a/fillit.h b/fillit.h index 413f2c0..17257da 100644 --- a/fillit.h +++ b/fillit.h @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */ -/* Updated: 2019/05/16 15:31:15 by hulamy ### ########.fr */ +/* Updated: 2019/05/17 17:03:35 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,6 +44,9 @@ typedef struct s_fillist int width; int height; int position; + int place; + int rank; + int num; int test; char letter; struct s_fillist *next; @@ -64,7 +67,7 @@ int add_to_list(char *square, t_fillist **list, char letter); void fill_list(char line[], t_fillist *list); void print_bits(unsigned int bits, int size); //TO DELETE BEFORE EVAL void print_tetri(unsigned int bits, int size); //TO DELETE BEFORE EVAL -void search_map(t_fillist *list); +int search_map(t_fillist *list); void print_map(unsigned int *tab, int width, int height, char letter); void print_final_map(t_fillist *list, int size); void ft_put_tetri_color(char c); diff --git a/search_map.c b/search_map.c index 7009f04..affaeb1 100644 --- a/search_map.c +++ b/search_map.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/16 16:50:29 by hulamy ### ########.fr */ +/* Updated: 2019/05/17 17:06:41 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,17 +16,20 @@ ** function that look if a tretri fit in a place */ -unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int n, int r) +unsigned int fit_in_place(unsigned int *map, t_fillist *lst, int size, int i) { unsigned int tmp; unsigned int mask; - int i; - unsigned int tetri; - i = list->height; - tetri = list->tetribit << 16 >> list->width; + int n; + int r; + + n = lst->num; + r = lst->rank; + i = lst->height; + tetri = lst->tetribit << 16 >> lst->width; tmp = 0; - mask = ~0u << (32 - list->width); + mask = ~0u << (32 - lst->width); while (i--) { if (tmp & tetri) @@ -34,7 +37,7 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int n, i if (r >= 32 && ++n) r -= 32; tmp = (mask & (map[n] << r)) | (mask & (map[n + 1] >> (32 - r))); - tetri <<= list->width; + tetri <<= lst->width; r += size; } return (!(tmp & tetri)); @@ -44,34 +47,31 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *list, int size, int n, i ** function that look for the first place in the map for a tetri */ -int find_place(unsigned int *tab, t_fillist *list, int size) +int find_place(unsigned int *map, t_fillist *list, int size) { - int place; // designe la position sur la ligne du tableau de size*size - int r; // designe la position de pos dans l'int du tableau - int n; // designe dans quel int tu tableau pos se trouve - int limit; // limit en hauteur du tableau a chercher pour la position du tetri + int limit; int pos; pos = list->position; - place = pos % size; - r = pos % 32; - n = pos / 32; + list->place = pos % size; + list->rank = pos % 32; + list->num = pos / 32; limit = (size - list->height + 1) * size; while (pos < limit) { - if (r >= 32 && ++n) - r -= 32; - if (place > size - list->width && (place = -1)) + if (list->rank >= 32 && ++list->num) + list->rank -= 32; + if (list->place > size - list->width) { + list->place = -1; pos += list->width - 2; - r += list->width - 2; + list->rank += list->width - 2; } - else if (fit_in_place(tab, list, size, n, r)) + else if (fit_in_place(map, list, size, 0)) return ((list->position = pos + 1)); -// return (pos + 1); pos++; - place++; - r++; + list->place++; + list->rank++; } return (0); } @@ -105,91 +105,34 @@ void add_remove(unsigned int *map, t_fillist *list, int size) ** function that recursively try to fill the map with the tetris */ -int fill_map(unsigned int *map, t_fillist *list, int size, t_fillist *link) // DEBUG link sert uniquement pour afficher le debug +int fill_map(unsigned int *map, t_fillist *list, int size) { -// /* -// int posx; -// int posy; - -// posx = list->posx; -// posy = list->posy; if (!list) return (1); - list->position = 0; // comme les positions sont marquees en dure dans la liste et non plus dans une petite variable - // au sein de la fonction, il faut remettre a zero la position a chaque nouveau tour de map + list->position = 0; while (find_place(map, list, size)) { -// pos = list->position; -// list->test = 1; // DEBUG pour afficher la map a chaque etape add_remove(map, list, size); -// print_final_map(link, size); ft_putnbrendl(pos); print_map(map, size, size, '#'); ft_putchar('\n'); // DEBUG - if (fill_map(map, list->next, size, link)) + if (fill_map(map, list->next, size)) return (1); add_remove(map, list, size); -// list->test = 0; // DEBUG pour afficher la map a chaque etape } -// */ - - /* - int pos; - - pos = 0; - if (!list) - return (1); - while ((pos = find_place(map, list, size, pos))) - { - add_remove(map, list, size, pos); - list->position = pos; - if (fill_map(map, list->next, size, link)) - return (1); - add_remove(map, list, size, pos); - } - */ - return (0); } -/* -** function that init the map to the right size fill with int equal to zeros -*/ - -unsigned int *init_map(int i) -{ - unsigned int *new; - int size; - - size = (i * i) / 32 + 1; - new = (unsigned int *)malloc(sizeof(*new) * size); - while (size) - new[size--] = 0; - return (new); -} - /* ** function that send to "fill_map" a map of a certain size and increment its size untill it's solved */ -void search_map(t_fillist *list) +int search_map(t_fillist *list) { t_fillist *tmp; - unsigned int *map; int size; int num; + int i; - size = 2; - num = 1; - tmp = list; - // trouve le nombre de tetri en parcourant la liste chainee - while ((tmp = tmp->next)) - num++; - // trouve la taille minimale de la map - while (size * size < num * 4) - size++; - map = init_map(size); - - - + /////////////////////////////////////////////////// TEST unsigned int print; tmp = list; while (tmp) @@ -197,15 +140,30 @@ void search_map(t_fillist *list) // imression pour tests print = tmp->tetribit; print <<= 16; - print_map(&print, tmp->width, tmp->height, tmp->letter); // test, imprime le tetri + print_map(&print, tmp->width, tmp->height, tmp->letter); ft_putchar('\n'); tmp = tmp->next; } + /////////////////////////////////////////////////// TEST - - // lance la recursive fill_map en augmentant la taille de la map tant qu'il n'y a pas de solution - while (!fill_map(map, list, size, list)) - map = init_map(size++); - print_final_map(list, size); // DEBUG - print_map(map, size, size, '#'); // DEBUG + size = 2; + num = 1; + tmp = list; + while ((tmp = tmp->next)) + num++; + while (size * size < num * 4) + size++; + i = 0; + while (!i) + { + num = (size * size) / 32 + 1; + if (!(map = (unsigned int *)malloc(sizeof(*map) * num))) + return (0); + while (num) + map[num--] = 0; + i = fill_map(map, list, size++); + } + print_final_map(list, size - 1); // DEBUG + print_map(map, size - 1, size - 1, '#'); // DEBUG + return (--size); }