1
- import math
1
+ import math
2
+
2
3
3
4
def is_valid (board , row , col , num , group_size ):
4
5
# Check if 'num' can be placed in the given position without violating the rules
@@ -8,14 +9,16 @@ def is_valid(board, row, col, num, group_size):
8
9
return False
9
10
10
11
# Check the group constraints
11
- group_start_row , group_start_col = row - row % group_size , col - col % group_size
12
+ group_start_row , group_start_col = row - \
13
+ row % group_size , col - col % group_size
12
14
for i in range (group_size ):
13
15
for j in range (group_size ):
14
16
if board [group_start_row + i ][group_start_col + j ] == num :
15
17
return False
16
18
17
19
return True
18
20
21
+
19
22
def find_empty_cell (board ):
20
23
# Find an empty cell (cell with value 0) on the board and return its coordinates
21
24
for i in range (len (board )):
@@ -24,6 +27,7 @@ def find_empty_cell(board):
24
27
return i , j
25
28
return None
26
29
30
+
27
31
def solve_kenken (board , group_size ):
28
32
# Function to solve the KenKen puzzle using backtracking
29
33
empty_cell = find_empty_cell (board )
@@ -44,6 +48,7 @@ def solve_kenken(board, group_size):
44
48
45
49
return False
46
50
51
+
47
52
def validate_puzzle_input (size , groups ):
48
53
# Function to validate the user-provided KenKen puzzle input
49
54
# Check for the following conditions:
@@ -63,25 +68,30 @@ def validate_puzzle_input(size, groups):
63
68
valid_operations = {'+' , '-' , '*' , '/' }
64
69
for target , operation in groups :
65
70
if target <= 0 :
66
- print ("Invalid target number. The target number should be greater than 0 for each group." )
71
+ print (
72
+ "Invalid target number. The target number should be greater than 0 for each group." )
67
73
return False
68
74
if operation not in valid_operations :
69
75
print ("Invalid operation. Valid operations are '+', '-', '*', or '/'." )
70
76
return False
71
77
72
78
return True
73
79
80
+
74
81
def get_puzzle_input ():
75
82
# Function to get the KenKen puzzle input from the user
76
83
size = int (input ("Enter the size of the grid (e.g., 5 for a 5x5 puzzle): " ))
77
- group_size = int (input ("Enter the size of each group (e.g., 3 for a standard 9x9 puzzle): " ))
84
+ group_size = int (
85
+ input ("Enter the size of each group (e.g., 3 for a standard 9x9 puzzle): " ))
78
86
79
87
groups = []
80
88
for i in range (size ):
81
89
while True :
82
90
try :
83
- group_target = int (input (f"Enter the target number for group { i + 1 } : " ))
84
- group_operation = input (f"Enter the operation for group { i + 1 } (+, -, *, /): " )
91
+ group_target = int (
92
+ input (f"Enter the target number for group { i + 1 } : " ))
93
+ group_operation = input (
94
+ f"Enter the operation for group { i + 1 } (+, -, *, /): " )
85
95
groups .append ((group_target , group_operation ))
86
96
break
87
97
except ValueError :
@@ -93,12 +103,14 @@ def get_puzzle_input():
93
103
print ("Invalid puzzle input. Please try again." )
94
104
return None
95
105
106
+
96
107
def print_board (board ):
97
108
# Function to pretty print the KenKen board
98
109
for row in board :
99
110
print (" " .join (str (num ) if num != 0 else "-" for num in row ))
100
111
print ()
101
112
113
+
102
114
def main ():
103
115
# Main function to run the KenKen puzzle solver
104
116
print ("KenKen Puzzle Solver" )
@@ -116,5 +128,6 @@ def main():
116
128
else :
117
129
print ("No solution exists." )
118
130
131
+
119
132
if __name__ == "__main__" :
120
133
main ()
0 commit comments