backtrack.py

This commit is contained in:
Timo Schmidt 2023-03-26 05:52:34 +02:00
parent 0a79846a4f
commit 24b7437128
1 changed files with 9 additions and 9 deletions

View File

@ -7,7 +7,7 @@ from time import sleep
# PADDING = int(os.popen('tput cols').read()) // 3 # '// 2 - 6' for exact middle for n=4 # PADDING = int(os.popen('tput cols').read()) // 3 # '// 2 - 6' for exact middle for n=4
PADDING = 5 PADDING = 5
DEBUG = False DEBUG = False
ITER = -1 # ITER = -1
def get_board_dim(board: list) -> int: def get_board_dim(board: list) -> int:
return int(len(board) ** 0.5) return int(len(board) ** 0.5)
@ -88,16 +88,16 @@ def check_constraint(slice: list, constraint: int) -> bool:
return True return True
def is_valid_state(board: list, border: list, next_candidate_index: int) -> bool: def is_valid_state(board: list, border: list, next_candidate_index: int) -> bool:
global ITER # global ITER
row = get_row(board, next_candidate_index) row = get_row(board, next_candidate_index)
column = get_col(board, next_candidate_index) column = get_col(board, next_candidate_index)
constraints = get_constraints(board, border, next_candidate_index) constraints = get_constraints(board, border, next_candidate_index)
ITER += 1 # ITER += 1
if ITER % 100000 == 0: # if ITER % 100000 == 0:
os.system('clear') # os.system('clear')
print_board(board, border) # print_board(board, border)
print(f'{ITER=}') # print(f'{ITER=}')
satisfies_constraints = all([ satisfies_constraints = all([
check_constraint(column, constraints[0]), check_constraint(column, constraints[0]),
check_constraint(row, constraints[2]), check_constraint(row, constraints[2]),
@ -176,6 +176,6 @@ if __name__ == '__main__':
# main('3 2 2 1 1 2 1 1 4 2 1 2 1 2 2 2') # henri's 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('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('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 # 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