From 2c81a37ce6954a013ad847c555de6a2a0c9dc218 Mon Sep 17 00:00:00 2001 From: Rafael Luis Yanase de Rezende Date: Thu, 11 Sep 2025 21:26:44 +0100 Subject: [PATCH 1/3] Update: print board state and hardcoded solutions --- include/board.h | 18 +++++++--- srcs/nqueens | Bin 0 -> 16288 bytes srcs/nqueens.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) create mode 100755 srcs/nqueens create mode 100644 srcs/nqueens.c diff --git a/include/board.h b/include/board.h index 95db114..8583686 100644 --- a/include/board.h +++ b/include/board.h @@ -6,17 +6,20 @@ /* By: rluis-ya +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/31 12:29:39 by rluis-ya #+# #+# */ -/* Updated: 2025/07/31 12:43:38 by rluis-ya ### ########.fr */ +/* Updated: 2025/09/11 21:11:39 by rluis-ya ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef BOARD_H #define BOARD_H -#include -typedef uint64_t Bitboard; +# include +# include +# include -typedef struct +typedef uint64_t Bitboard; // 64 squares for board representation + +typedef struct s_board { Bitboard pieces[2][6]; //Squares occupied by each piece (6) for each color (2) @@ -24,4 +27,11 @@ typedef struct Bitboard ocuppiedColor[2]; //Ocuppied squares by color } t_board; +typedef struct s_state +{ + int *board; + int size; + struct s_state *next; +} t_state; + #endif diff --git a/srcs/nqueens b/srcs/nqueens new file mode 100755 index 0000000000000000000000000000000000000000..3feba71ab215cbd4a0fb95c070307ef98be52790 GIT binary patch literal 16288 zcmeHO4Qw366`r#bVhY5U;6QK?UfhHRs4sSK3;|4>pLNz%Lel)Gttf2PcWd8?^WAZ` zS8Phal?%dh)4Hud0jX*Pm0AQ6p{-h^QZP-56H2Q&1quLaJ2lSbN{R@4YuO`(}S;yfb&|o7yWP5hbZoU8h`WfycD5V&9cwp;Vi?O3lZw zrs~ugpy%LdV|5Utm7YSditUIdzwfXVh^$ALSpN1dB^FK{dHr&Sb&DMW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LseiaU3qGjAgOu7CET;y%=ChM*41B!^KI{O$umUTmgU^xe z@x0R%%VIgY)tNFKt6PC6IK{56SX@mlT{oQ`Gfs=w3$RRN&F*xz(@L8OCz~&rRx_Vo0@#V47tbf%eRZYkP!rhq27 zi}20Ptd&o|seRkp&AO>yzrn-``VF7Ee(mOt^`P-zJySPWYi!u2%#EA2tXsRu+|u5@ zUEg8uSi5eMZn|3m+GG(wt=H#m0n@nm?G7kwvDp6u7x>!=%dwPu-e}YGZ3yK;nt5A6 znV@!f8xE#Xh&oF1whN%N>3avvL?LS?txSUU6?ANYRU(x!iv=4ovNIH@&O(8G-3<=3 z?;%h;HYZ3RY1hJxTM?%3iWw!x$lDuyi4PCpgqk(P^uh}{5DzUn4uYB)f^-7aG znql>%P?1+OdO*c+m-OJ`_uZKPWtPT&G-kqW?Izi0CD_QkfQ*wwrqyPC|9&3$2-5%W z{YjpK!mULHkF0Y2v+eT}d$RQ>dVjx*F_*Mo#-)N%e|enhcSe3Y4%!DI{xWVA)VM+# zPEuCRU&fC&fTL3ue;Ef09)QlDg6)q(Nn=X!mvN+^)FbT|JHZ_MX^bkej8g^Mxbk58 znO1iqLwzFtGHwaUzzmDx>94dgKS7F*Ke4*}`nn%VzyB~B2#URwA+q3u0snoh5Zon>jN&KwNWfp-|AKc* z#{~SP*vA9@GA zf2-!13O--<^fGe&2>n}R{LdemKL~1K$QZ2u)qwxC7kh?+xexJw2YQMTKlaMwf7urr zY_G_IG!eq6uk0`L?!65lk;KlErexvILh$I`k@{u4FZ+XKAF$kC;wRVNx8O_h7k`;| z53lfQ44%LE3)aBd@2}cCKiNnu^)UOs^Qnb1l_mAd_%zz+c}P9tPj&fI9oNt1WtI3} zQf9o2`1?_=UBBs7T;K#ZjOLp(HHH + +static +void print_board(int *board_state, int size); + +int main(int argc, char **argv) +{ + t_state this; + int i; + //int solution4[4] = {2, 0, 3, 1}; + //int solution5[5] = {0, 2, 4, 1, 3}; + //int solution6[6] = {1, 3, 5, 0, 2, 4}; + //int solution7[7] = {0, 2, 4, 6, 1, 3, 5}; + //int solution8[8] = {1, 3, 5, 7, 0, 2, 4, 6}; + + if (argc != 2) + return (-1); + init_state(argc, argv); + /* + At start: + column index -> -1 -1 -1 -1 + row index -> 0 1 2 3 + */ + i = -1; + while (++i < problem_size) + board_state[i] = -1; + //print_board(board_state, problem_size); + //print_board(solution4, 4); + //print_board(solution5, 5); + //print_board(solution6, 6); + //print_board(solution7, 7); + //print_board(solution8, 8); + free(board_state); +} + +static +void print_board(int *board_state, int size) +{ + int i; + int j; + + i = 0; + while (i < size) + { + j = 0; + printf("\n"); + while (j < size) + { + if (i == board_state[j]) + printf(" \u2655 "); + else + printf(" * "); + j++; + } + i++; + } + printf("\n"); + printf("\n"); +} + +static +int init_state(char **argv, t_state **state) +{ + int size; + int i; + + *state = (t_state *)calloc(1, sizeof(t_state)); + if (!*state) + return (-1); + size = atoi(argv[1]); + (*state)->board = (int *)calloc(size, sizeof(int)); + if (!(*state)->board) + return (-1); + i = 0; + while (i < size) + { + (*state)->board[i] = -1; + i++; + } + (*state)->next = NULL; +} + + + + + + + + + + From 8770a0714270ddc79c65f250e15214d0e13653f1 Mon Sep 17 00:00:00 2001 From: Rafael Luis Yanase de Rezende Date: Sun, 14 Sep 2025 19:35:59 +0100 Subject: [PATCH 2/3] Update: improved fibonacci --- Makefile | 22 +++++++- include/board.h | 12 +++- srcs/nqueens | Bin 16288 -> 0 bytes srcs/nqueens.c | 130 ++++++++++++++++++++++--------------------- srcs/nqueens_utils.c | 76 +++++++++++++++++++++++++ srcs/recursion.c | 114 +++++++++++++++++++++++++++++++++++++ 6 files changed, 287 insertions(+), 67 deletions(-) delete mode 100755 srcs/nqueens create mode 100644 srcs/nqueens_utils.c create mode 100644 srcs/recursion.c diff --git a/Makefile b/Makefile index ca4d710..5c5f6dd 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,35 @@ NAME= autoMate +NQ = n_queens + +RECUR = fib + CC = cc -CFLAGS = -Wall -Werror -Wextra -Iinclude +CFLAGS = -Wall -Werror -Wextra -Iinclude -g SRCS_DIR = ./srcs/main.c -OBJS_DIR = objs +NQ_PATH = ./srcs/nqueens.c #./srcs/nqueens_utils.c + +RECUR_PATH = ./srcs/recursion.c INC_DIR = include all: $(NAME) +$(NQ): + $(CC) $(CFLAGS) -o $(NQ) $(NQ_PATH) + +$(RECUR): + $(CC) $(CFLAGS) -o $(RECUR) $(RECUR_PATH) + $(NAME): $(SRCS_DIR) $(CC) $(CFLAGS) -o $(NAME) $(SRCS_DIR) +clean: + @rm -f $(NQ) + @rm -f $(RECUR) + @rm -f $(NAME) + +.PHONY: all n_queens clean diff --git a/include/board.h b/include/board.h index 8583686..2ef5088 100644 --- a/include/board.h +++ b/include/board.h @@ -6,7 +6,7 @@ /* By: rluis-ya +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/31 12:29:39 by rluis-ya #+# #+# */ -/* Updated: 2025/09/11 21:11:39 by rluis-ya ### ########.fr */ +/* Updated: 2025/09/12 02:00:10 by rluis-ya ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,9 @@ # include # include # include +# include +# include +# include typedef uint64_t Bitboard; // 64 squares for board representation @@ -27,11 +30,18 @@ typedef struct s_board Bitboard ocuppiedColor[2]; //Ocuppied squares by color } t_board; +/* N QUEENS FUNCTIONS */ + typedef struct s_state { int *board; int size; struct s_state *next; + struct s_state *prev; } t_state; +void print_board(int *board_state, int size); +int init_state(char **argv, t_state **state); +t_state *new_state(t_state *old_board); +int cleanup(t_state **state); #endif diff --git a/srcs/nqueens b/srcs/nqueens deleted file mode 100755 index 3feba71ab215cbd4a0fb95c070307ef98be52790..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16288 zcmeHO4Qw366`r#bVhY5U;6QK?UfhHRs4sSK3;|4>pLNz%Lel)Gttf2PcWd8?^WAZ` zS8Phal?%dh)4Hud0jX*Pm0AQ6p{-h^QZP-56H2Q&1quL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board.h" -static -void print_board(int *board_state, int size); +int isvalid(int *queens, int n, int i); +void print_result(int *queens, int size); +void algorithm(int *queens, int size, int nth_queen, int *flag); int main(int argc, char **argv) { - t_state this; - int i; - //int solution4[4] = {2, 0, 3, 1}; - //int solution5[5] = {0, 2, 4, 1, 3}; - //int solution6[6] = {1, 3, 5, 0, 2, 4}; - //int solution7[7] = {0, 2, 4, 6, 1, 3, 5}; - //int solution8[8] = {1, 3, 5, 7, 0, 2, 4, 6}; + int *queens; + int size; + int found_solution; if (argc != 2) return (-1); - init_state(argc, argv); - /* - At start: - column index -> -1 -1 -1 -1 - row index -> 0 1 2 3 - */ - i = -1; - while (++i < problem_size) - board_state[i] = -1; - //print_board(board_state, problem_size); - //print_board(solution4, 4); - //print_board(solution5, 5); - //print_board(solution6, 6); - //print_board(solution7, 7); - //print_board(solution8, 8); - free(board_state); + found_solution = 0; + size = atoi(argv[1]); + queens = (int *)malloc(size * sizeof(int)); + memset(queens, -1, size * sizeof(int)); + algorithm(queens, size, 0, &found_solution); + if (!found_solution) + printf("No solution...\n"); + free(queens); + return (0); } -static -void print_board(int *board_state, int size) +void algorithm(int *queens, int size, int nth_queen, int *flag) { - int i; - int j; + int col; - i = 0; - while (i < size) + if (nth_queen == size) { - j = 0; - printf("\n"); - while (j < size) + print_result(queens, size); + *flag = 1; + return ; + } + col = 0; + while (col < size) + { + if (isvalid(queens, nth_queen, col)) { - if (i == board_state[j]) - printf(" \u2655 "); - else - printf(" * "); - j++; + queens[nth_queen] = col; + algorithm(queens, size, nth_queen + 1, flag); } - i++; + col++; } - printf("\n"); - printf("\n"); } -static -int init_state(char **argv, t_state **state) +int isvalid(int *queens, int row, int col) { - int size; int i; - *state = (t_state *)calloc(1, sizeof(t_state)); - if (!*state) - return (-1); - size = atoi(argv[1]); - (*state)->board = (int *)calloc(size, sizeof(int)); - if (!(*state)->board) - return (-1); i = 0; - while (i < size) + while (i < row) { - (*state)->board[i] = -1; + if (queens[i] == col || abs(row - i) == abs(col - queens[i])) + return (0); i++; } - (*state)->next = NULL; + return (1); } +void print_result(int *queens, int size) +{ + int i; + i = 0; + while (i < size) + { + printf("%d ", queens[i]); + i++; + } + printf("\n"); +} +/* +int main(int argc, char **argv) +{ + t_state *this; + //int solution4[4] = {2, 0, 3, 1}; + //int solution5[5] = {0, 2, 4, 1, 3}; + //int solution6[6] = {1, 3, 5, 0, 2, 4}; + //int solution7[7] = {0, 2, 4, 6, 1, 3, 5}; + //int solution8[8] = {1, 3, 5, 7, 0, 2, 4, 6}; - - - - - - - + if (argc != 2) + return (-1); + init_state(argv, &this); + At start: + row index -> -1 -1 -1 -1 + column index -> 0 1 2 3 + //print_board(board_state, problem_size); + print_board(this->board, this->size); + cleanup(&this); +} +*/ diff --git a/srcs/nqueens_utils.c b/srcs/nqueens_utils.c new file mode 100644 index 0000000..d54ab24 --- /dev/null +++ b/srcs/nqueens_utils.c @@ -0,0 +1,76 @@ +#include "board.h" +#include + +void print_board(int *board_state, int size) +{ + int i; + int j; + + i = 0; + while (i < size) + { + j = 0; + printf("\n"); + while (j < size) + { + if (i == board_state[j]) + printf(" \u2655 "); + else + printf(" * "); + j++; + } + i++; + } + printf("\n"); + printf("\n"); +} + +int init_state(char **argv, t_state **state) +{ + int i; + + *state = (t_state *)calloc(1, sizeof(t_state)); + if (!*state) + return (-1); + (*state)->size = atoi(argv[1]); + (*state)->board = (int *)calloc((*state)->size, sizeof(int)); + if (!(*state)->board) + return (-1); + i = -1; + while (++i < (*state)->size) + (*state)->board[i] = -1; + (*state)->next = NULL; + (*state)->prev = NULL; + return (0); +} + +t_state *new_state(t_state **old_board) +{ + t_state *new; + + if (!*old_board) + return (NULL); + memcpy(new, *old_board, sizeof(t_state)); +} + +int cleanup(t_state **state) +{ + if (!*state) + return (-1); + if ((*state)->board) + free((*state)->board); + if ((*state)->next) + free((*state)->next); + if ((*state)->prev) + free((*state)->prev); + free(*state); + return (0); +} + + + + + + + + diff --git a/srcs/recursion.c b/srcs/recursion.c new file mode 100644 index 0000000..04869a3 --- /dev/null +++ b/srcs/recursion.c @@ -0,0 +1,114 @@ +# include +# include +# include +# include + +typedef struct s_exp +{ + int *explored; + int size; +} t_exp; + +// Driver functions +int fib(int n); +int ofib(int *explored, int n); +// Helper functions +int isexplored(int *explored, int n); +int init(t_exp **set, int size); +void print_explored(int *explored, int size); +void clean(t_exp **set); + +int main(int argc, char **argv) +{ + t_exp *set; + int target; + int result; + + if (argc != 2) + { + printf("Wrong number of arguments\n"); + return (-1); + } + // Recursive fibonacci sequence (raw) + target = atoi(argv[1]); + set = NULL; + if (init(&set, target) < 0) + return (-1); +// result = fib(target); +// printf("\nResult %d\n", result); + // Recursive fibonacci sequence (improved searched states) + result = ofib(set->explored, target); + printf("\nResult %d\n", result); + clean(&set); + return (0); +} + +int init(t_exp **set, int size) +{ + *set = (t_exp *)malloc(sizeof(t_exp)); + if (!*set) + return (-1); + (*set)->explored = (int *)malloc(size * sizeof(int)); + if (!(*set)->explored) + return (free(*set), -1); + memset((*set)->explored, -1, size * sizeof(int)); + (*set)->size = size; + (*set)->explored[0] = 1; + (*set)->explored[1] = 1; + return (0); +} + +int fib(int n) +{ + if (n == 1 || n == 2) + { + //printf("f(%d) = 1 f(%d) = 1\n", 1, 2); + return (1); + } + //printf("f(%d) need f(%d), f(%d)\n", n, n - 1, n - 2); + return (fib(n - 1) + fib(n - 2)); +} + +int ofib(int *explored, int n) +{ + if (n <= 0) + return (0); + if (isexplored(explored, n)) + return (explored[n - 1]); + explored[n - 2] = ofib(explored, n - 1); + explored[n - 3] = ofib(explored, n - 2); + return (explored[n - 2] + explored[n - 3]); +} + +int isexplored(int *explored, int n) +{ + if (explored[n - 1] > 0) + return (1); + return (0); +} + +void print_explored(int *explored, int size) +{ + int i; + + i = 0; + printf("Explored set: {"); + while (i < size) + { + if (i == size - 1) + printf(" %d ", explored[i]); + else + printf(" %d ,", explored[i]); + i++; + } + printf("}\n"); +} + +void clean(t_exp **set) +{ + if (!*set) + return ; + if ((*set)->explored) + free((*set)->explored); + free(*set); +} From 5dd0c580f87e267f650b1d92a340d1a0d5317a0f Mon Sep 17 00:00:00 2001 From: Rafael Rezende Date: Fri, 19 Sep 2025 21:09:27 +0100 Subject: [PATCH 3/3] Fix: variable name swaped --- srcs/nqueens.c | 18 +++++++++--------- srcs/recursion.c | 12 ++++++++---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/srcs/nqueens.c b/srcs/nqueens.c index ed8246a..ac245a1 100644 --- a/srcs/nqueens.c +++ b/srcs/nqueens.c @@ -25,7 +25,7 @@ int main(int argc, char **argv) void algorithm(int *queens, int size, int nth_queen, int *flag) { - int col; + int row; if (nth_queen == size) { @@ -33,26 +33,26 @@ void algorithm(int *queens, int size, int nth_queen, int *flag) *flag = 1; return ; } - col = 0; - while (col < size) + row = 0; + while (row < size) { - if (isvalid(queens, nth_queen, col)) + if (isvalid(queens, nth_queen, row)) { - queens[nth_queen] = col; + queens[nth_queen] = row; algorithm(queens, size, nth_queen + 1, flag); } - col++; + row++; } } -int isvalid(int *queens, int row, int col) +int isvalid(int *queens, int col, int row) { int i; i = 0; - while (i < row) + while (i < col) { - if (queens[i] == col || abs(row - i) == abs(col - queens[i])) + if (queens[i] == row || abs(col - i) == abs(row - queens[i])) return (0); i++; } diff --git a/srcs/recursion.c b/srcs/recursion.c index 04869a3..8732a52 100644 --- a/srcs/recursion.c +++ b/srcs/recursion.c @@ -34,8 +34,8 @@ int main(int argc, char **argv) set = NULL; if (init(&set, target) < 0) return (-1); -// result = fib(target); -// printf("\nResult %d\n", result); + //result = fib(target); + //printf("\nResult %d\n", result); // Recursive fibonacci sequence (improved searched states) result = ofib(set->explored, target); printf("\nResult %d\n", result); @@ -62,10 +62,10 @@ int fib(int n) { if (n == 1 || n == 2) { - //printf("f(%d) = 1 f(%d) = 1\n", 1, 2); + printf("f(%d) = 1 f(%d) = 1\n", 1, 2); return (1); } - //printf("f(%d) need f(%d), f(%d)\n", n, n - 1, n - 2); + printf("f(%d) need f(%d), f(%d)\n", n, n - 1, n - 2); return (fib(n - 1) + fib(n - 2)); } @@ -74,9 +74,13 @@ int ofib(int *explored, int n) if (n <= 0) return (0); if (isexplored(explored, n)) + { + printf("f(%d) = 1 f(%d) = 1\n", 1, 2); return (explored[n - 1]); + } explored[n - 2] = ofib(explored, n - 1); explored[n - 3] = ofib(explored, n - 2); + printf("f(%d) need f(%d), f(%d)\n", n, n - 1, n - 2); return (explored[n - 2] + explored[n - 3]); }