Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
28 changes: 24 additions & 4 deletions include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,42 @@
/* By: rluis-ya <rluis-ya@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 12:29:39 by rluis-ya #+# #+# */
/* Updated: 2025/07/31 12:43:38 by rluis-ya ### ########.fr */
/* Updated: 2025/09/12 02:00:10 by rluis-ya ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef BOARD_H
#define BOARD_H

#include <stdint.h>
typedef uint64_t Bitboard;
# include <stdint.h>
# include <unistd.h>
# include <stdlib.h>
# include <stdio.h>
# include <math.h>
# include <string.h>

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)

Bitboard ocuppied; //All ocuppied squares (both black and white)
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);
Comment thread
RafaelyRezende marked this conversation as resolved.
int cleanup(t_state **state);
#endif
94 changes: 94 additions & 0 deletions srcs/nqueens.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "board.h"

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)
{
int *queens;
int size;
int found_solution;

if (argc != 2)
return (-1);
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);
}

void algorithm(int *queens, int size, int nth_queen, int *flag)
{
int row;

if (nth_queen == size)
{
print_result(queens, size);
*flag = 1;
return ;
}
row = 0;
while (row < size)
{
if (isvalid(queens, nth_queen, row))
{
queens[nth_queen] = row;
algorithm(queens, size, nth_queen + 1, flag);
}
row++;
}
}

int isvalid(int *queens, int col, int row)
{
int i;

i = 0;
while (i < col)
{
if (queens[i] == row || abs(col - i) == abs(row - queens[i]))
return (0);
i++;
}
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);
}
*/
76 changes: 76 additions & 0 deletions srcs/nqueens_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "board.h"
#include <stdio.h>

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));
Comment thread
RafaelyRezende marked this conversation as resolved.
Comment thread
RafaelyRezende marked this conversation as resolved.
}

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);
Comment thread
RafaelyRezende marked this conversation as resolved.
return (0);
}








118 changes: 118 additions & 0 deletions srcs/recursion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <string.h>

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))
{
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);
Comment thread
RafaelyRezende marked this conversation as resolved.
printf("f(%d) need f(%d), f(%d)\n", n, n - 1, 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);
}