norminetted backtrack.c and get_cur_pos.c

This commit is contained in:
Steffen Matthes 2023-03-26 22:51:09 +02:00
parent d340b998cb
commit 226e625e16
2 changed files with 55 additions and 41 deletions

View File

@ -1,4 +1,14 @@
#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); int is_valid_state(int **board, int *borders, int *pos, int n);
void get_cur_pos(int **board, int *pos, int n); void get_cur_pos(int **board, int *pos, int n);
@ -7,14 +17,17 @@ 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]; int pos[2];
int y;
int x;
int counter;
get_cur_pos(board, pos, n); get_cur_pos(board, pos, n);
int y = pos[0]; y = pos[0];
int x = pos[1]; x = pos[1];
if (rec_depth == n * n) if (rec_depth == n * n)
{ {
return (1); return (1);
} }
int counter = 0;
while (++counter < n + 1) while (++counter < n + 1)
{ {
board[y][x] = counter; board[y][x] = counter;

View File

@ -6,16 +6,17 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */ /* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 21:56:39 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 <stdio.h>
void get_cur_pos(int **board, int *pos, int n) void get_cur_pos(int **board, int *pos, int n)
{ {
int i = -1; int i;
int j = -1; int j;
i = -1;
j = -1;
while (++i < n) while (++i < n)
{ {
while (++j < n) while (++j < n)
@ -24,10 +25,10 @@ void get_cur_pos(int **board, int *pos, int n)
{ {
pos[0] = i; pos[0] = i;
pos[1] = j; pos[1] = j;
return; return ;
} }
} }
j = -1; j = -1;
} }
return; return ;
} }