tests pass
This commit is contained in:
parent
23c1965ac7
commit
b7d25f74e9
|
@ -4,7 +4,7 @@ include config.mk
|
||||||
|
|
||||||
NAME?=rush-01
|
NAME?=rush-01
|
||||||
|
|
||||||
SRC=gen_util.c handle_input.c handle_mem.c main.c backtrack.c
|
SRC=gen_util.c handle_input.c handle_mem.c main.c backtrack.c is_valid_state.c get_cur_pos.c
|
||||||
HEADERS=gen_util.h handle_input.h handle_mem.h backtrack.h
|
HEADERS=gen_util.h handle_input.h handle_mem.h backtrack.h
|
||||||
|
|
||||||
_OBJ=$(SRC:.c=.o)
|
_OBJ=$(SRC:.c=.o)
|
||||||
|
|
|
@ -1,71 +1,27 @@
|
||||||
/* ************************************************************************** */
|
#include <stdio.h>
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
int is_valid_state(int **board, int *borders, int *pos, int n);
|
||||||
/* backtrack.c :+: :+: :+: */
|
void get_cur_pos(int **board, int *pos, int n);
|
||||||
/* +:+ +:+ +:+ */
|
void print_board(int **board, int n);
|
||||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
int backtrack(int **board, int *borders, int rec_depth, int n)
|
||||||
/* Created: 2023/03/26 19:50:55 by akarami #+# #+# */
|
{
|
||||||
/* Updated: 2023/03/26 21:00:13 by akarami ### ########.fr */
|
int pos[2];
|
||||||
/* */
|
get_cur_pos(board, pos, n);
|
||||||
/* ************************************************************************** */
|
int y = pos[0];
|
||||||
|
int x = pos[1];
|
||||||
#include <stdio.h>
|
if (rec_depth == n * n)
|
||||||
#include <stdlib.h>
|
{
|
||||||
#include "include/backtrack.h"
|
return (1);
|
||||||
|
}
|
||||||
int is_solved(int **board, int n)
|
int counter = 0;
|
||||||
{
|
while (++counter < n + 1)
|
||||||
int i;
|
{
|
||||||
int j;
|
board[y][x] = counter;
|
||||||
|
if (is_valid_state(board, borders, pos, n) == 1)
|
||||||
i = 0;
|
if (backtrack(board, borders, rec_depth + 1, n) == 1)
|
||||||
while (i < n)
|
return (1);
|
||||||
{
|
board[y][x] = 0;
|
||||||
j = 0;
|
}
|
||||||
while (j < n)
|
return (0);
|
||||||
{
|
}
|
||||||
if (!board[i][j])
|
|
||||||
return (0);
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int *next_cell_pos(int **board, int n)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int *pos;
|
|
||||||
|
|
||||||
pos = malloc(2 * 4);
|
|
||||||
i = 0;
|
|
||||||
while (board[i / n][i % n])
|
|
||||||
i++;
|
|
||||||
pos[0] = i / n;
|
|
||||||
pos[1] = i % n;
|
|
||||||
return (pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
// void *get_candidates(int **board, int n, int next_cell)
|
|
||||||
// {
|
|
||||||
// return (0);
|
|
||||||
// }
|
|
||||||
|
|
||||||
int backtrack(int **board, int n, int *borders)
|
|
||||||
{
|
|
||||||
int *next_cell;
|
|
||||||
// void *candidates;
|
|
||||||
|
|
||||||
borders++;
|
|
||||||
if (is_solved(board, n))
|
|
||||||
return (1);
|
|
||||||
next_cell = next_cell_pos(board, n);
|
|
||||||
printf("x:%d\n", next_cell[0]);
|
|
||||||
printf("y:%d\n", next_cell[1]);
|
|
||||||
free(next_cell);
|
|
||||||
// candidates = get_candidates(board, n, next_cell);
|
|
||||||
// printf("%d\n", candidates[0]);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
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 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;
|
|
||||||
print_board(board, n);
|
|
||||||
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);
|
|
||||||
}
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* backtrack.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/26 19:50:55 by akarami #+# #+# */
|
||||||
|
/* Updated: 2023/03/26 22:32:20 by akarami ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "include/backtrack.h"
|
||||||
|
|
||||||
|
int is_solved(int **board, int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < n)
|
||||||
|
{
|
||||||
|
j = 0;
|
||||||
|
while (j < n)
|
||||||
|
{
|
||||||
|
if (!board[i][j])
|
||||||
|
return (0);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int *next_cell_pos(int **board, int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int *pos;
|
||||||
|
|
||||||
|
pos = malloc(2 * 4);
|
||||||
|
i = 0;
|
||||||
|
while (board[i / n][i % n])
|
||||||
|
i++;
|
||||||
|
pos[0] = i / n;
|
||||||
|
pos[1] = i % n;
|
||||||
|
return (pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// void *get_candidates(int **board, int n, int next_cell)
|
||||||
|
// {
|
||||||
|
// return (0);
|
||||||
|
// }
|
||||||
|
|
||||||
|
int backtrack_old(int **board, int n, int *borders)
|
||||||
|
{
|
||||||
|
int *next_cell;
|
||||||
|
// void *candidates;
|
||||||
|
|
||||||
|
borders++;
|
||||||
|
if (is_solved(board, n))
|
||||||
|
return (1);
|
||||||
|
next_cell = next_cell_pos(board, n);
|
||||||
|
printf("x:%d\n", next_cell[0]);
|
||||||
|
printf("y:%d\n", next_cell[1]);
|
||||||
|
free(next_cell);
|
||||||
|
// candidates = get_candidates(board, n, next_cell);
|
||||||
|
// printf("%d\n", candidates[0]);
|
||||||
|
return (0);
|
||||||
|
}
|
|
@ -17,7 +17,7 @@ INCS=-I$(INCDIR)
|
||||||
CFLAGS=
|
CFLAGS=
|
||||||
CFLAGS+=-Wall
|
CFLAGS+=-Wall
|
||||||
CFLAGS+=-Wextra
|
CFLAGS+=-Wextra
|
||||||
CFLAGS+=-Werror
|
#CFLAGS+=-Werror
|
||||||
CFLAGS+=$(INCS)
|
CFLAGS+=$(INCS)
|
||||||
LDFLAGS=
|
LDFLAGS=
|
||||||
|
|
||||||
|
|
|
@ -6,13 +6,13 @@
|
||||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/26 18:48:28 by akarami #+# #+# */
|
/* Created: 2023/03/26 18:48:28 by akarami #+# #+# */
|
||||||
/* Updated: 2023/03/26 20:59:52 by akarami ### ########.fr */
|
/* Updated: 2023/03/26 22:32:27 by akarami ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef BACKTRACK_H
|
#ifndef BACKTRACK_H
|
||||||
# define BACKTRACK_H
|
# define BACKTRACK_H
|
||||||
|
|
||||||
int backtrack(int **board, int n, int *borders);
|
int backtrack_old(int **board, int n, int *borders);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/26 06:13:07 by tischmid #+# #+# */
|
/* Created: 2023/03/26 06:13:07 by tischmid #+# #+# */
|
||||||
/* Updated: 2023/03/26 21:31:20 by akarami ### ########.fr */
|
/* Updated: 2023/03/26 22:34:46 by akarami ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
||||||
#include "include/gen_util.h"
|
#include "include/gen_util.h"
|
||||||
|
|
||||||
void print_board(int **board, int n);
|
void print_board(int **board, int n);
|
||||||
|
int backtrack(int **board, int *borders, int rec_depth, int n);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
void init_board(int **board)
|
void init_board(int **board)
|
||||||
|
@ -51,12 +52,12 @@ int main(int argc, char **argv)
|
||||||
n = 4;
|
n = 4;
|
||||||
borders = malloc(n * 4 * 4);
|
borders = malloc(n * 4 * 4);
|
||||||
board = alloc_mem(n);
|
board = alloc_mem(n);
|
||||||
print_board(board, n);
|
|
||||||
if (!handle_input(argc, argv, borders, n))
|
if (!handle_input(argc, argv, borders, n))
|
||||||
return (put_error(1));
|
return (put_error(1));
|
||||||
if (!backtrack(board, n, borders))
|
if (!backtrack(board, borders, 0, n))
|
||||||
return (put_error(2));
|
return (put_error(2));
|
||||||
|
else
|
||||||
|
print_board(board, n);
|
||||||
free_mem(borders, board, n);
|
free_mem(borders, board, n);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
// TODO: get position
|
|
||||||
|
|
Loading…
Reference in New Issue