Compare commits
14 Commits
6ceda4e900
...
226e625e16
| Author | SHA1 | Date |
|---|---|---|
|
|
226e625e16 | |
|
|
d340b998cb | |
|
|
b7d25f74e9 | |
|
|
23c1965ac7 | |
|
|
47fd54f893 | |
|
|
73b1e189c4 | |
|
|
a50c0a146f | |
|
|
2a205bd79e | |
|
|
5202f43395 | |
|
|
ef055f0456 | |
|
|
faaa653c17 | |
|
|
8e2c3ab995 | |
|
|
39046d0ef5 | |
|
|
13e398dfd7 |
|
|
@ -1,2 +1,3 @@
|
|||
obj/
|
||||
build/
|
||||
*.swp
|
||||
|
|
|
|||
|
|
@ -4,14 +4,15 @@ include config.mk
|
|||
|
||||
NAME?=rush-01
|
||||
|
||||
SRC=gen_util.c handle_input.c handle_mem.c main.c print_array.c
|
||||
HEADERS=
|
||||
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
|
||||
|
||||
_OBJ=$(SRC:.c=.o)
|
||||
OBJ=$(patsubst %,$(OBJDIR)/%,$(_OBJ))
|
||||
DEPS=$(patsubst %,$(INCDIR)/%,$(HEADERS))
|
||||
|
||||
.PHONY: clean install uninstall fclean all re makedirs
|
||||
DEPS=$(patsubst %,$(INCDIR)/%,$(HEADERS))
|
||||
OBJ=$(patsubst %,$(OBJDIR)/%,$(_OBJ))
|
||||
|
||||
.PHONY: clean install uninstall fclean all re test
|
||||
|
||||
all: $(BUILDDIR)/$(NAME)
|
||||
|
||||
|
|
@ -19,18 +20,16 @@ $(BUILDDIR)/$(NAME): $(OBJ)
|
|||
$(CC) $(LDFLAGS) -o $@ $^
|
||||
|
||||
$(OBJDIR)/%.o: %.c $(DEPS)
|
||||
norminette $<
|
||||
# norminette $<
|
||||
$(CC) $(CFLAGS) -o $@ -c $<
|
||||
|
||||
clean:
|
||||
$(RM) $(wildcard $(OBJ))
|
||||
$(RM) $(wildcard $(INCDIR)/*~)
|
||||
$(RM) $(wildcard *~)
|
||||
$(RMDIR) $(wildcard $(OBJDIR))
|
||||
$(RM) $(OBJ)
|
||||
$(RM) $(INCDIR)/*~
|
||||
$(RM) *~
|
||||
|
||||
fclean: clean
|
||||
$(RM) $(wildcard $(BUILDDIR)/*)
|
||||
$(RMDIR) $(wildcard $(BUILDDIR))
|
||||
$(RM) $(BUILDDIR)/*
|
||||
|
||||
install: all
|
||||
$(MKDIR) $(DESTDIR)$(PREFIX)/bin
|
||||
|
|
@ -41,3 +40,13 @@ uninstall:
|
|||
$(RM) $(DESTDIR)$(PREFIX)/bin/$(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
SUCCESS=printf '\n\033[32m%s\033[m\n\n' "Success"
|
||||
FAIL=printf '\n\033[31m%s\033[m\n\n' "Fail"
|
||||
test: re
|
||||
$(BUILDDIR)/$(NAME) && $(FAIL) || $(SUCCESS)
|
||||
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2 " && $(FAIL) || $(SUCCESS)
|
||||
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 6" && $(FAIL) || $(SUCCESS)
|
||||
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 a" && $(FAIL) || $(SUCCESS)
|
||||
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2" whatever && $(FAIL) || $(SUCCESS)
|
||||
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2" && $(SUCCESS) || $(FAIL)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 backtrack(int **board, int *borders, int rec_depth, int n)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
|
@ -26,7 +26,6 @@ CC=cc
|
|||
|
||||
# other executables
|
||||
RM=/bin/rm -f
|
||||
RMDIR=/bin/rmdir --ignore-fail-on-non-empty
|
||||
MKDIR=/bin/mkdir -p
|
||||
CP=/bin/cp -f
|
||||
CHMOD=/bin/chmod
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@
|
|||
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/25 19:41:44 by smatthes #+# #+# */
|
||||
/* Updated: 2023/03/26 06:13:02 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/03/26 21:31:51 by akarami ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
#include "include/gen_util.h"
|
||||
|
||||
int str_len(char *str)
|
||||
{
|
||||
|
|
@ -22,9 +23,10 @@ int str_len(char *str)
|
|||
return (i);
|
||||
}
|
||||
|
||||
void put_error(void)
|
||||
int put_error(int exit_code)
|
||||
{
|
||||
write(1, "Error\n", 6);
|
||||
return (exit_code);
|
||||
}
|
||||
|
||||
int *find_position(int **board, int n, int *pos)
|
||||
|
|
@ -48,3 +50,29 @@ int *find_position(int **board, int n, int *pos)
|
|||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void ft_putchar(char c)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
}
|
||||
|
||||
void print_board(int **board, int n)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
x = 0;
|
||||
while (x < n)
|
||||
{
|
||||
y = 0;
|
||||
while (y < n)
|
||||
{
|
||||
ft_putchar(board[x][y] + '0');
|
||||
if (y != n)
|
||||
ft_putchar(' ');
|
||||
++y;
|
||||
}
|
||||
ft_putchar('\n');
|
||||
++x;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,41 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print_array.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: akarami <akarami@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/25 17:15:08 by akarami #+# #+# */
|
||||
/* Updated: 2023/03/26 07:48:42 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_putchar(char c)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
}
|
||||
|
||||
void ft_print_arrays(int **board, int size)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
x = 1;
|
||||
while (x <= size)
|
||||
{
|
||||
y = 1;
|
||||
while (y <= size)
|
||||
{
|
||||
ft_putchar(board[x][y]);
|
||||
if (y != size)
|
||||
{
|
||||
ft_putchar(' ');
|
||||
}
|
||||
++y;
|
||||
}
|
||||
ft_putchar('\n');
|
||||
++x;
|
||||
}
|
||||
}
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_cur_pos.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/26 21:56:39 by smatthes #+# #+# */
|
||||
/* Updated: 2023/03/26 22:49:45 by smatthes ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
void get_cur_pos(int **board, int *pos, int n)
|
||||
{
|
||||
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 ;
|
||||
}
|
||||
|
|
@ -6,11 +6,12 @@
|
|||
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/25 18:58:21 by smatthes #+# #+# */
|
||||
/* Updated: 2023/03/26 07:55:41 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/03/26 19:49:33 by akarami ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "include/handle_input.h"
|
||||
#include "include/gen_util.h"
|
||||
|
||||
// handle sum of opposite inputs
|
||||
int handle_input(int argc, char **argv, int *borders, int n)
|
||||
|
|
|
|||
|
|
@ -6,22 +6,29 @@
|
|||
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/25 19:45:42 by smatthes #+# #+# */
|
||||
/* Updated: 2023/03/26 07:58:12 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/03/26 21:28:15 by akarami ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "include/handle_mem.h"
|
||||
|
||||
int **alloc_mem(int **board, int n)
|
||||
// Allocates memory and initialized with 0's
|
||||
int **alloc_mem(int n)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int **board;
|
||||
|
||||
board = malloc(n * 8);
|
||||
i = -1;
|
||||
while (++i < n)
|
||||
{
|
||||
board[i] = malloc(4);
|
||||
board[i] = malloc(n * 4);
|
||||
j = -1;
|
||||
while (++j < n)
|
||||
board[i][j] = 0;
|
||||
}
|
||||
return (board);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* backtrack.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/26 18:48:28 by akarami #+# #+# */
|
||||
/* Updated: 2023/03/26 22:32:27 by akarami ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef BACKTRACK_H
|
||||
# define BACKTRACK_H
|
||||
|
||||
int backtrack_old(int **board, int n, int *borders);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* gen_util.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/26 19:35:52 by akarami #+# #+# */
|
||||
/* Updated: 2023/03/26 21:32:10 by akarami ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GEN_UTIL_H
|
||||
# define GEN_UTIL_H
|
||||
|
||||
int str_len(char *str);
|
||||
int put_error(int exit_code);
|
||||
void ft_putchar(char c);
|
||||
int *find_position(int **board, int n, int *pos);
|
||||
void print_board(int **board, int n);
|
||||
|
||||
#endif
|
||||
|
|
@ -6,15 +6,15 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/26 07:48:00 by tischmid #+# #+# */
|
||||
/* Updated: 2023/03/26 07:48:15 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/03/26 20:00:10 by akarami ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef HANDLE_INPUT_H
|
||||
# define HANDLE_INPUT_H
|
||||
|
||||
int handle_input(int argc, char **argv, int *borders, int n);
|
||||
int check_inp_valid(int argc, char **argv, int n);
|
||||
int str_len(char *str);
|
||||
void atoi_arr(char **argv, int *borders, int n);
|
||||
int check_opposite_sum(int *borders, int n);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,21 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.h :+: :+: :+: */
|
||||
/* handle_mem.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/26 07:43:49 by tischmid #+# #+# */
|
||||
/* Updated: 2023/03/26 07:58:02 by tischmid ### ########.fr */
|
||||
/* Created: 2023/03/26 19:46:53 by akarami #+# #+# */
|
||||
/* Updated: 2023/03/26 20:36:34 by akarami ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MAIN_H
|
||||
# define MAIN_H
|
||||
#ifndef HANDLE_MEM_H
|
||||
# define HANDLE_MEM_H
|
||||
|
||||
int **alloc_mem(int **board, int n);
|
||||
int **alloc_mem(int n);
|
||||
void free_mem(int *borders, int **board, int n);
|
||||
int handle_input(int argc, char **argv, int *borders, int n);
|
||||
void put_error(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,165 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int check_unique_row(int **board, int *borders, int *pos, int n);
|
||||
int check_unique_col(int **board, int *borders, int *pos, int n);
|
||||
int check_view(int **board, int *borders, int *pos, int n);
|
||||
int calc_num_seen_row(int **board, int *pos, int n, int from_left_right);
|
||||
int calc_num_seen_col(int **board, int *pos, int n, int from_top_bottom);
|
||||
int check_view_col(int **board, int *borders, int *pos, int n);
|
||||
int check_view_row(int **board, int *borders, int *pos, int n);
|
||||
|
||||
int is_valid_state(int **board, int *borders, int *pos, int n)
|
||||
{
|
||||
int row_check = check_unique_row(board, borders, pos, n);
|
||||
int col_check = check_unique_col(board, borders, pos, n);
|
||||
int view_check = check_view(board, borders, pos, n);
|
||||
if (row_check && col_check && view_check)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int check_unique_row(int **board, int *borders, int *pos, int n)
|
||||
{
|
||||
int y = pos[0];
|
||||
int x = pos[1];
|
||||
int board_val = board[y][x];
|
||||
int i = -1;
|
||||
while (++i < x)
|
||||
{
|
||||
if (board[y][i] == board_val)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int check_unique_col(int **board, int *borders, int *pos, int n)
|
||||
{
|
||||
int y = pos[0];
|
||||
int x = pos[1];
|
||||
int board_val = board[y][x];
|
||||
int i = -1;
|
||||
while (++i < y)
|
||||
{
|
||||
if (board[i][x] == board_val)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int check_view(int **board, int *borders, int *pos, int n)
|
||||
{
|
||||
int y = pos[0];
|
||||
int x = pos[1];
|
||||
int check_row = 1;
|
||||
int check_col = 1;
|
||||
|
||||
if(x % n == n - 1)
|
||||
{
|
||||
check_row = check_view_row(board, borders, pos, n);
|
||||
}
|
||||
if(y % n == n - 1)
|
||||
{
|
||||
check_col = check_view_col(board, borders, pos, n);
|
||||
}
|
||||
if (!check_col || !check_row)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int check_view_row(int **board, int *borders, int *pos, int n)
|
||||
{
|
||||
int y = pos[0];
|
||||
int x = pos[1];
|
||||
int bord_val_left = borders[y % n + 2 * n];
|
||||
int bord_val_right = borders[y % n + 3 * n];
|
||||
int res_left_right = calc_num_seen_row(board, pos, n, 1);
|
||||
int res_right_left = calc_num_seen_row(board, pos, n, -1);
|
||||
if (bord_val_left == res_left_right && bord_val_right == res_right_left)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int check_view_col(int **board, int *borders, int *pos, int n)
|
||||
{
|
||||
int y = pos[0];
|
||||
int x = pos[1];
|
||||
int bord_val_top = borders[x % n];
|
||||
int bord_val_bott = borders[x % n + n];
|
||||
int res_top_bott = calc_num_seen_col(board, pos, n, 1);
|
||||
int res_bott_top = calc_num_seen_col(board, pos, n, -1);
|
||||
if (bord_val_top == res_top_bott && bord_val_bott == res_bott_top)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int calc_num_seen_row(int **board, int *pos, int n, int from_left_right)
|
||||
{
|
||||
int y = pos[0];
|
||||
int num_seen = 0;
|
||||
int i;
|
||||
int max = 0;
|
||||
if (from_left_right == 1)
|
||||
{
|
||||
i = -1;
|
||||
while (++i < n)
|
||||
{
|
||||
if (board[y][i] > max)
|
||||
{
|
||||
num_seen += 1;
|
||||
max = board[y][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (from_left_right == -1)
|
||||
{
|
||||
i = n;
|
||||
while (--i >= 0)
|
||||
{
|
||||
if (board[y][i] > max)
|
||||
{
|
||||
num_seen += 1;
|
||||
max = board[y][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return num_seen;
|
||||
}
|
||||
|
||||
int calc_num_seen_col(int **board, int *pos, int n, int from_top_bottom)
|
||||
{
|
||||
int x = pos[1];
|
||||
int num_seen = 0;
|
||||
int i;
|
||||
int max = 0;
|
||||
if (from_top_bottom == 1)
|
||||
{
|
||||
i = -1;
|
||||
while (++i < n)
|
||||
{
|
||||
if (board[i][x] > max)
|
||||
{
|
||||
num_seen += 1;
|
||||
max = board[i][x];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (from_top_bottom == -1)
|
||||
{
|
||||
i = n;
|
||||
while (--i >= 0)
|
||||
{
|
||||
if (board[i][x] > max)
|
||||
{
|
||||
num_seen += 1;
|
||||
max = board[i][x];
|
||||
}
|
||||
}
|
||||
}
|
||||
return num_seen;
|
||||
}
|
||||
52
ex00/main.c
52
ex00/main.c
|
|
@ -6,13 +6,42 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/26 06:13:07 by tischmid #+# #+# */
|
||||
/* Updated: 2023/03/26 07:57:54 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/03/26 22:34:46 by akarami ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include "include/main.h"
|
||||
#include <stdio.h>
|
||||
#include "include/handle_mem.h"
|
||||
#include "include/handle_input.h"
|
||||
#include "include/backtrack.h"
|
||||
#include "include/gen_util.h"
|
||||
|
||||
void print_board(int **board, int n);
|
||||
int backtrack(int **board, int *borders, int rec_depth, int n);
|
||||
|
||||
/*
|
||||
void init_board(int **board)
|
||||
{
|
||||
board[0][0] = 1;
|
||||
board[0][1] = 2;
|
||||
board[0][2] = 3;
|
||||
board[0][3] = 4;
|
||||
board[1][0] = 2;
|
||||
board[1][1] = 3;
|
||||
board[1][2] = 4;
|
||||
board[1][3] = 1;
|
||||
board[2][0] = 3;
|
||||
board[2][1] = 4;
|
||||
board[2][2] = 1;
|
||||
board[2][3] = 2;
|
||||
board[3][0] = 4;
|
||||
board[3][1] = 1;
|
||||
board[3][2] = 2;
|
||||
board[3][3] = 3;
|
||||
board[3][3] = 3;
|
||||
}
|
||||
*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
|
@ -22,18 +51,13 @@ int main(int argc, char **argv)
|
|||
|
||||
n = 4;
|
||||
borders = malloc(n * 4 * 4);
|
||||
board = alloc_mem(board, n);
|
||||
board = alloc_mem(n);
|
||||
if (!handle_input(argc, argv, borders, n))
|
||||
{
|
||||
put_error();
|
||||
return (1);
|
||||
}
|
||||
if (!backtrack(borders, board, n))
|
||||
{
|
||||
put_error();
|
||||
return (1);
|
||||
}
|
||||
return (put_error(1));
|
||||
if (!backtrack(board, borders, 0, n))
|
||||
return (put_error(2));
|
||||
else
|
||||
print_board(board, n);
|
||||
free_mem(borders, board, n);
|
||||
return (0);
|
||||
}
|
||||
// TODO: get position
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from time import sleep
|
|||
|
||||
# PADDING = int(os.popen('tput cols').read()) // 3 # '// 2 - 6' for exact middle for n=4
|
||||
PADDING = 5
|
||||
DEBUG = False
|
||||
DEBUG = True
|
||||
|
||||
def get_board_dim(board: list) -> int:
|
||||
return int(len(board) ** 0.5)
|
||||
|
|
@ -122,7 +122,7 @@ def backtrack_skyscrapers(board: list, sols: list, border: list) -> None:
|
|||
board[next_cell_idx] = candidate
|
||||
if DEBUG:
|
||||
input()
|
||||
# os.system('clear')
|
||||
os.system('clear')
|
||||
print_board(board, border)
|
||||
if is_valid_state(board, border, next_cell_idx):
|
||||
backtrack_skyscrapers(board, sols, border)
|
||||
|
|
@ -161,8 +161,8 @@ if __name__ == '__main__':
|
|||
# main('3 2 1 1 2 2 3 2 1 1 2 2') # 3 x 3
|
||||
# main('4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2') # original problem
|
||||
# main('3 2 2 1 1 2 1 1 4 2 1 2 1 2 2 2') # henri's problem
|
||||
# main('2 1 2 3 3 2 3 1 3 2 2 1 3 3 2 3 4 2 1 2') # 5 x 5
|
||||
main('2 1 2 3 3 2 3 1 3 2 2 1 3 3 2 3 4 2 1 2') # 5 x 5
|
||||
# main('1 2 2 4 3 5 4 4 2 2 2 1 1 2 3 4 2 4 5 3 3 2 2 1') # 6 x 6
|
||||
# main('6 3 1 3 3 3 2 1 2 3 3 3 3 3 3 7 3 4 3 2 1 2 1 2 2 3 3 4') # 7 x 7
|
||||
main('7 4 2 3 3 2 1 1 2 2 2 3 4 6 6 5 4 2 3 2 1 1 2 2 4 2 4 4') # 7 x 7
|
||||
# main('7 4 2 3 3 2 1 1 2 2 2 3 4 6 6 5 4 2 3 2 1 1 2 2 4 2 4 4') # 7 x 7
|
||||
# main('4 3 4 1 5 4 3 2 2 4 2 4 1 3 5 4 3 3 5 2 3 1 3 2 2 1 2 3 2 4 3 3') # 8 x 8
|
||||
|
|
|
|||
|
|
@ -1,70 +0,0 @@
|
|||
#include "helpers.h"
|
||||
#include "backtrack.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int is_solved(int board[SIZE][SIZE])
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (i < SIZE)
|
||||
{
|
||||
j = 0;
|
||||
while (j < SIZE)
|
||||
{
|
||||
if (!board[i][j])
|
||||
return (0);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int next_cell_pos(int board[SIZE][SIZE])
|
||||
{
|
||||
int pos;
|
||||
|
||||
pos = 0;
|
||||
while (board[pos / SIZE][pos % SIZE])
|
||||
pos++;
|
||||
return (pos);
|
||||
}
|
||||
|
||||
void *get_candidates(int board[SIZE][SIZE], int next_cell)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
|
||||
int backtrack(int board[SIZE][SIZE], int clues[SIZE * 4])
|
||||
{
|
||||
int next_cell;
|
||||
void *candidates;
|
||||
|
||||
if (is_solved(board))
|
||||
return (1);
|
||||
next_cell = next_cell_pos(board);
|
||||
candidates = get_candidates(board, next_cell);
|
||||
printf("%d\n", candidates[0];
|
||||
return (0);
|
||||
}
|
||||
|
||||
void print_board(int board[SIZE][SIZE])
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = -1;
|
||||
while (++i < SIZE)
|
||||
{
|
||||
j = -1;
|
||||
while (++j < SIZE)
|
||||
{
|
||||
ft_putchar(board[i][j] + '0');
|
||||
ft_putchar(' ');
|
||||
}
|
||||
ft_putchar('\b');
|
||||
ft_putchar('\n');
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
#ifndef BACKTRACK_H
|
||||
# define BACKTRACK_H
|
||||
|
||||
int backtrack(int board[SIZE][SIZE], int clues[SIZE]);
|
||||
void print_board(int board[SIZE][SIZE]);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
#include "helpers.h"
|
||||
|
||||
int is_valid_input(int argc, char **argv)
|
||||
{
|
||||
char *input;
|
||||
int valid_input_len;
|
||||
int i;
|
||||
|
||||
if (argc != 2)
|
||||
return (0);
|
||||
input = argv[1];
|
||||
valid_input_len = SIZE * 4 * 2 - 1;
|
||||
if (ft_strlen(input) != valid_input_len)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (i < valid_input_len)
|
||||
{
|
||||
if (i % 2 == 0 && (input[i] < '1' || input[i] > '4'))
|
||||
return (0);
|
||||
if (i % 2 == 1 && input[i] != ' ')
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
#ifndef ERROR_CHECK_H
|
||||
# define ERROR_CHECK_H
|
||||
# include "helpers.h"
|
||||
|
||||
int is_valid_input(int argc, char **argv);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
#include <unistd.h>
|
||||
#include "helpers.h"
|
||||
|
||||
void ft_putchar(char c)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
}
|
||||
|
||||
void init_board(int board[SIZE][SIZE])
|
||||
{
|
||||
// int i;
|
||||
// int j;
|
||||
|
||||
// i = -1;
|
||||
// while (++i < SIZE)
|
||||
// {
|
||||
// j = -1;
|
||||
// while (++j < SIZE)
|
||||
// board[i][j] = 0;
|
||||
// }
|
||||
board[0][0] = 1;
|
||||
board[0][1] = 2;
|
||||
board[0][2] = 3;
|
||||
board[0][3] = 4;
|
||||
board[1][0] = 2;
|
||||
board[1][1] = 3;
|
||||
board[1][2] = 4;
|
||||
board[1][3] = 1;
|
||||
board[2][0] = 3;
|
||||
board[2][1] = 4;
|
||||
board[2][2] = 1;
|
||||
board[2][3] = 2;
|
||||
board[3][0] = 4;
|
||||
board[3][1] = 1;
|
||||
board[3][2] = 2;
|
||||
board[3][3] = 3;
|
||||
board[3][3] = 0;
|
||||
}
|
||||
|
||||
int ft_puterr(int exit_code)
|
||||
{
|
||||
write(1, "Error\n", 6);
|
||||
return (exit_code);
|
||||
}
|
||||
int ft_strlen(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
void get_clues(char *input, int *border)
|
||||
{
|
||||
int i;
|
||||
int len;
|
||||
|
||||
len = ft_strlen(input);
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
*border++ = input[i] - '0';
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
#ifndef HELPERS_H
|
||||
# define HELPERS_H
|
||||
# define SIZE 4
|
||||
|
||||
void ft_putchar(char c);
|
||||
int ft_strlen(char *str);
|
||||
int ft_puterr(int exit_code);
|
||||
void init_board(int board[SIZE][SIZE]);
|
||||
void get_clues(char *input, int *border);
|
||||
|
||||
#endif
|
||||
|
||||
18
tmp/main.c
18
tmp/main.c
|
|
@ -1,18 +0,0 @@
|
|||
#include "helpers.h"
|
||||
#include "error_check.h"
|
||||
#include "backtrack.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int board[SIZE][SIZE];
|
||||
int clues[SIZE * 4];
|
||||
|
||||
if (!is_valid_input(argc, argv))
|
||||
return (ft_puterr(1));
|
||||
get_clues(argv[1], clues);
|
||||
init_board(board);
|
||||
if (!backtrack(board, clues))
|
||||
return (ft_puterr(2));
|
||||
print_board(board);
|
||||
return (0);
|
||||
}
|
||||
Loading…
Reference in New Issue