Compare commits

...

2 Commits

Author SHA1 Message Date
Timo Schmidt 845e6dcb81 backtrack.py 2023-03-26 06:59:17 +02:00
Timo Schmidt 38eff0354b Normed 2023-03-26 06:59:03 +02:00
6 changed files with 93 additions and 87 deletions

View File

@ -6,11 +6,11 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 19:41:44 by smatthes #+# #+# */
/* Updated: 2023/03/25 22:52:55 by smatthes ### ########.fr */
/* Updated: 2023/03/26 06:13:02 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include<unistd.h>
#include <unistd.h>
int str_len(char *str)
{
@ -22,29 +22,29 @@ int str_len(char *str)
return (i);
}
void put_error()
void put_error(void)
{
write(1,"Error\n",6);
write(1, "Error\n", 6);
}
int *find_position(int **board, int n, int *pos)
int *find_position(int **board, int n, int *pos)
{
int i;
int j;
int i;
int j;
i = -1;
j = -1;
while(++i < n)
while (++i < n)
{
while(++j < n)
if(board[i][j] == 0)
while (++j < n)
{
if (board[i][j] == 0)
{
pos[0] = i;
pos[1] = j;
return pos;
return (pos);
}
}
}
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 18:58:21 by smatthes #+# #+# */
/* Updated: 2023/03/25 23:29:41 by smatthes ### ########.fr */
/* Updated: 2023/03/26 06:57:31 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
@ -63,14 +63,13 @@ void atoi_arr(char **argv, int *borders, int n)
}
}
int check_opposite_sum(int *borders, int n)
int check_opposite_sum(int *borders, int n)
{
int i;
int val;
int opp_val;
int val;
int opp_val;
i = 0;
i = 0;
while (i < 3 * n)
{
val = borders[1];
@ -79,7 +78,7 @@ int check_opposite_sum(int *borders, int n)
{
return (0);
}
i++;
++i;
if (i == n)
i = 2 * n;
}

View File

@ -6,33 +6,32 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 19:45:42 by smatthes #+# #+# */
/* Updated: 2023/03/25 22:41:35 by smatthes ### ########.fr */
/* Updated: 2023/03/26 06:58:14 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
# include<stdlib.h>
# include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
int **allo_mem(int **board, int n)
int **allo_mem(int **board, int n)
{
int i;
int i;
board = malloc(n * 8);
i = -1;
while(++i < n)
while (++i < n)
{
board[i] = malloc(4);
}
return board;
return (board);
}
void free_mem(int *borders, int **board, int n)
void free_mem(int *borders, int **board, int n)
{
int i;
int i;
i = -1;
while(++i < n)
i = -1;
while (++i < n)
{
free(board[i]);
}

View File

@ -1,31 +1,39 @@
# include<unistd.h>
# include<stdio.h>
# include<stdlib.h>
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 06:13:07 by tischmid #+# #+# */
/* Updated: 2023/03/26 06:58:29 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
int **allo_mem(int **board, 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();
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int **allo_mem(int **board, 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);
int main(int argc, char **argv)
int main(int argc, char **argv)
{
int n;
int *borders;
int **board;
int n;
int *borders;
int **board;
n = 4;
borders = malloc(n * 4 * 4);
board = allo_mem(board, n);
// handle sum of opposite inputs
board = allo_mem(board, n);
// handle sum of opposite inputs
if (handle_input(argc, argv, borders, n) == 0)
{
put_error();
return (1);
}
/*
if (backtrack(borders, board, n) == 0)
{
@ -35,8 +43,7 @@ int main(int argc, char **argv)
print_stuff
*/
free_mem(borders, board, n);
free_mem(borders, board, n);
return (0);
}
// to do get position
// TODO: get position

View File

@ -6,7 +6,7 @@
/* By: akarami <akarami@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 17:15:08 by akarami #+# #+# */
/* Updated: 2023/03/25 18:15:52 by akarami ### ########.fr */
/* Updated: 2023/03/26 06:11:37 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,9 +21,9 @@ void ft_print_arrays(int arr_matrix, int size)
{
int x;
int y;
x = 1;
while(x <= size)
while (x <= size)
{
y = 1;
while (y <= size)
@ -39,10 +39,10 @@ void ft_print_arrays(int arr_matrix, int size)
}
}
int main(int ac, char** av)
int main(int ac, char **av)
{
int arr_matrix;
int size;
int arr_matrix;
int size;
ft_print_arrays(arr_matrix, size);
}
}

View File

@ -35,10 +35,10 @@ def get_col(board: list, pos: int) -> list:
]
return column
def get_candidates(board: list, next_candidate_index: int) -> int:
def get_candidates(board: list, next_cell_idx: int) -> list:
all_candidates = set(range(1 + get_board_dim(board)))
row = get_row(board, next_candidate_index)
column = get_col(board, next_candidate_index)
row = get_row(board, next_cell_idx)
column = get_col(board, next_cell_idx)
for candidate in all_candidates.copy():
if candidate in row:
@ -49,7 +49,7 @@ def get_candidates(board: list, next_candidate_index: int) -> int:
continue
return all_candidates
def get_constraints(board: list, border: list, pos: int) -> list:
def get_clues(board: list, border: list, pos: int) -> list:
board_dim = get_board_dim(board)
col_idx = pos // board_dim
row_idx = pos % board_dim
@ -57,11 +57,11 @@ def get_constraints(board: list, border: list, pos: int) -> list:
coldown = border[row_idx + board_dim]
rowleft = border[col_idx + board_dim * 2]
rowright = border[col_idx + board_dim * 3]
constraints = [colup, coldown, rowleft, rowright]
return constraints
clues = [colup, coldown, rowleft, rowright]
return clues
def check_constraint(slice: list, constraint: int) -> bool:
constraint = int(constraint)
def check_clue(slice: list, clue: int) -> bool:
clue = int(clue)
peak = 0
visible_towers = 0
@ -73,26 +73,27 @@ def check_constraint(slice: list, constraint: int) -> bool:
if tower > peak:
peak = tower
visible_towers += 1
if visible_towers > constraint:
if visible_towers > clue:
return False
if constraint + tower > board_dim + 1 + i:
if clue + tower > board_dim + 1 + i:
return False
if 0 not in slice:
return visible_towers == constraint
return visible_towers == clue
else:
return True
def is_valid_state(board: list, border: list, next_candidate_index: int) -> bool:
row = get_row(board, next_candidate_index)
column = get_col(board, next_candidate_index)
constraints = get_constraints(board, border, next_candidate_index)
satisfies_constraints = all([
check_constraint(column, constraints[0]),
check_constraint(row, constraints[2]),
check_constraint(column[::-1], constraints[1]),
check_constraint(row[::-1], constraints[3]),
def is_valid_state(board: list, border: list, next_cell_idx: int) -> bool:
clues = get_clues(board, border, next_cell_idx)
row = get_row(board, next_cell_idx)
column = get_col(board, next_cell_idx)
satisfies_clues = all([
check_clue(column, clues[0]),
check_clue(row, clues[2]),
check_clue(column[::-1], clues[1]),
check_clue(row[::-1], clues[3]),
])
return satisfies_constraints
return satisfies_clues
def print_board(board: list, border: list) -> None:
padding = ' ' * PADDING
@ -115,17 +116,17 @@ def backtrack_skyscrapers(board: list, sols: list, border: list) -> None:
sols.append(board)
print_board(sols[0], border)
sys.exit(0)
next_candidate_index = board.index(0)
candidates = get_candidates(board, next_candidate_index)
next_cell_idx = board.index(0)
candidates = get_candidates(board, next_cell_idx)
for candidate in candidates:
board[next_candidate_index] = candidate
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_candidate_index):
if is_valid_state(board, border, next_cell_idx):
backtrack_skyscrapers(board, sols, border)
board[next_candidate_index] = 0
board[next_cell_idx] = 0
def main(*args, **kwargs) -> None:
if kwargs:
@ -163,5 +164,5 @@ if __name__ == '__main__':
# 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('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
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