From 23c1965ac7b4ab68ccf2a373ec19af5aa7f4d439 Mon Sep 17 00:00:00 2001 From: Steffen Matthes Date: Sun, 26 Mar 2023 22:29:28 +0200 Subject: [PATCH] new files --- ex00/backtrack.c.new | 28 ++++++ ex00/get_cur_pos.c | 33 +++++++ ex00/is_valid_state.c | 165 ++++++++++++++++++++++++++++++++++ python_prototype/backtrack.py | 8 +- 4 files changed, 230 insertions(+), 4 deletions(-) create mode 100644 ex00/backtrack.c.new create mode 100644 ex00/get_cur_pos.c create mode 100644 ex00/is_valid_state.c diff --git a/ex00/backtrack.c.new b/ex00/backtrack.c.new new file mode 100644 index 0000000..221e6fc --- /dev/null +++ b/ex00/backtrack.c.new @@ -0,0 +1,28 @@ +#include + +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); +} diff --git a/ex00/get_cur_pos.c b/ex00/get_cur_pos.c new file mode 100644 index 0000000..a3271e8 --- /dev/null +++ b/ex00/get_cur_pos.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_cur_pos.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: smatthes +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/26 21:56:39 by smatthes #+# #+# */ +/* Updated: 2023/03/26 21:56:44 by smatthes ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +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; +} diff --git a/ex00/is_valid_state.c b/ex00/is_valid_state.c new file mode 100644 index 0000000..0655621 --- /dev/null +++ b/ex00/is_valid_state.c @@ -0,0 +1,165 @@ +#include + +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; +} diff --git a/python_prototype/backtrack.py b/python_prototype/backtrack.py index 21d613b..97ec543 100755 --- a/python_prototype/backtrack.py +++ b/python_prototype/backtrack.py @@ -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