norminetted backtrack.c and get_cur_pos.c
This commit is contained in:
parent
d340b998cb
commit
226e625e16
|
@ -1,27 +1,40 @@
|
|||
#include <stdio.h>
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* backtrack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
@ -6,28 +6,29 @@
|
|||
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdio.h>
|
||||
|
||||
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 ;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue