Amazing-Python-Scripts
133 строки · 4.1 Кб
1import math2
3
4def is_valid(board, row, col, num, group_size):5# Check if 'num' can be placed in the given position without violating the rules6# Check row and column7for i in range(len(board)):8if board[i][col] == num or board[row][i] == num:9return False10
11# Check the group constraints12group_start_row, group_start_col = row - \13row % group_size, col - col % group_size14for i in range(group_size):15for j in range(group_size):16if board[group_start_row + i][group_start_col + j] == num:17return False18
19return True20
21
22def find_empty_cell(board):23# Find an empty cell (cell with value 0) on the board and return its coordinates24for i in range(len(board)):25for j in range(len(board)):26if board[i][j] == 0:27return i, j28return None29
30
31def solve_kenken(board, group_size):32# Function to solve the KenKen puzzle using backtracking33empty_cell = find_empty_cell(board)34if not empty_cell:35# If no empty cell found, the board is solved36return True37
38row, col = empty_cell39
40for num in range(1, len(board) + 1):41if is_valid(board, row, col, num, group_size):42board[row][col] = num43
44if solve_kenken(board, group_size):45return True46
47board[row][col] = 048
49return False50
51
52def validate_puzzle_input(size, groups):53# Function to validate the user-provided KenKen puzzle input54# Check for the following conditions:55# 1. The board size is valid (greater than 1 and a perfect square)56# 2. The number of groups is equal to the board size57# 3. Each group has a valid target number (greater than 0)58# 4. Each group operation is valid (+, -, *, /)59
60if size <= 1 or int(size ** 0.5) ** 2 != size:61print("Invalid board size. The size should be greater than 1 and a perfect square.")62return False63
64if len(groups) != size:65print(f"Invalid number of groups. Expected {size} groups.")66return False67
68valid_operations = {'+', '-', '*', '/'}69for target, operation in groups:70if target <= 0:71print(72"Invalid target number. The target number should be greater than 0 for each group.")73return False74if operation not in valid_operations:75print("Invalid operation. Valid operations are '+', '-', '*', or '/'.")76return False77
78return True79
80
81def get_puzzle_input():82# Function to get the KenKen puzzle input from the user83size = int(input("Enter the size of the grid (e.g., 5 for a 5x5 puzzle): "))84group_size = int(85input("Enter the size of each group (e.g., 3 for a standard 9x9 puzzle): "))86
87groups = []88for i in range(size):89while True:90try:91group_target = int(92input(f"Enter the target number for group {i + 1}: "))93group_operation = input(94f"Enter the operation for group {i + 1} (+, -, *, /): ")95groups.append((group_target, group_operation))96break97except ValueError:98print("Invalid input. Please enter a valid target number and operation.")99
100if validate_puzzle_input(size, groups):101return size, group_size, groups102else:103print("Invalid puzzle input. Please try again.")104return None105
106
107def print_board(board):108# Function to pretty print the KenKen board109for row in board:110print(" ".join(str(num) if num != 0 else "-" for num in row))111print()112
113
114def main():115# Main function to run the KenKen puzzle solver116print("KenKen Puzzle Solver")117
118puzzle_input = get_puzzle_input()119if puzzle_input:120size, group_size, groups = puzzle_input121
122# Initialize the board with zeros123kenken_board = [[0 for _ in range(size)] for _ in range(size)]124
125if solve_kenken(kenken_board, group_size):126print("Solved KenKen Puzzle:")127print_board(kenken_board)128else:129print("No solution exists.")130
131
132if __name__ == "__main__":133main()134