diff --git a/ex00/backtrack.c b/ex00/backtrack.c index b07cebc..0e32d0c 100644 --- a/ex00/backtrack.c +++ b/ex00/backtrack.c @@ -1,27 +1,40 @@ -#include +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* backtrack.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: smatthes +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/26 22:44:15 by smatthes #+# #+# */ +/* Updated: 2023/03/26 22:47:19 by smatthes ### ########.fr */ +/* */ +/* ************************************************************************** */ -int is_valid_state(int **board, int *borders, int *pos, int n); -void get_cur_pos(int **board, int *pos, int n); -void print_board(int **board, int n); +int is_valid_state(int **board, int *borders, int *pos, int n); +void get_cur_pos(int **board, int *pos, int n); +void print_board(int **board, int n); -int backtrack(int **board, int *borders, int rec_depth, int n) +int backtrack(int **board, int *borders, int rec_depth, int n) { - int pos[2]; - get_cur_pos(board, pos, n); - int y = pos[0]; - int x = pos[1]; - if (rec_depth == n * n) - { - return (1); - } - int counter = 0; - while (++counter < n + 1) - { - board[y][x] = counter; - if (is_valid_state(board, borders, pos, n) == 1) - if (backtrack(board, borders, rec_depth + 1, n) == 1) - return (1); + int pos[2]; + int y; + int x; + int counter; + + get_cur_pos(board, pos, n); + y = pos[0]; + x = pos[1]; + if (rec_depth == n * n) + { + return (1); + } + while (++counter < n + 1) + { + board[y][x] = counter; + if (is_valid_state(board, borders, pos, n) == 1) + if (backtrack(board, borders, rec_depth + 1, n) == 1) + return (1); board[y][x] = 0; - } - return (0); + } + return (0); } diff --git a/ex00/get_cur_pos.c b/ex00/get_cur_pos.c index a3271e8..7fe0a30 100644 --- a/ex00/get_cur_pos.c +++ b/ex00/get_cur_pos.c @@ -6,28 +6,29 @@ /* By: smatthes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/26 21:56:39 by smatthes #+# #+# */ -/* Updated: 2023/03/26 21:56:44 by smatthes ### ########.fr */ +/* Updated: 2023/03/26 22:49:45 by smatthes ### ########.fr */ /* */ /* ************************************************************************** */ -#include - void get_cur_pos(int **board, int *pos, int n) { - int i = -1; - int j = -1; - while (++i < n) - { - while (++j < n) - { - if (board[i][j] == 0) - { - pos[0] = i; - pos[1] = j; - return; - } - } - j = -1; - } - return; + int i; + int j; + + i = -1; + j = -1; + while (++i < n) + { + while (++j < n) + { + if (board[i][j] == 0) + { + pos[0] = i; + pos[1] = j; + return ; + } + } + j = -1; + } + return ; }