-
Notifications
You must be signed in to change notification settings - Fork 3
Feature/nqueens #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RafaelyRezende
wants to merge
3
commits into
main
Choose a base branch
from
feature/nqueens
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
|
RafaelyRezende marked this conversation as resolved.
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); | ||
|
RafaelyRezende marked this conversation as resolved.
|
||
| return (0); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
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); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.