-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
59 lines (48 loc) · 1.92 KB
/
test.py
File metadata and controls
59 lines (48 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import copy
def valid(configuration):
"""
Checks whether the configuration of the board is a valid solution.
It must hold that:
1.) No row contains the same number
2.) No column contains the same number
3.) No square contains the same number
"""
rows = [set() for _ in range(9)]
columns = [set() for _ in range(9)]
squares = [[set() for r in range(3)] for c in range(3)]
for r in range(9):
for c in range(9):
digit = configuration[r][c]
if digit in rows[r] or digit in columns[c] or digit in squares[r//3][c//3]:
return False
rows[r].add(digit)
columns[c].add(digit)
squares[r//3][c//3].add(digit)
return True
def solve(configuration, determine_uniqueness=False):
board = copy.deepcopy(configuration)
solutionCount = 0
def solve_h():
nonlocal solutionCount, determine_uniqueness
def possible_placement(r, c, n):
if (any(board[r][i]==n for i in range(9)) or
any(board[i][c]==n for i in range(9)) or
any(board[i][j]==n for i in range(3*(r//3), 3*(r//3)+3)
for j in range(3*(c//3), 3*(c//3)+3))):
return False
return True
for r in range(9):
for c in range(9):
if board[r][c] == 0:
for n in range(1, 10):
if possible_placement(r, c, n):
board[r][c] = n
if solve_h():
if not determine_uniqueness or solutionCount > 1: # Stop if more than one solution found
return True
board[r][c] = 0 # Backtrack
return False
solutionCount += 1 # Increment solution count
return True
solve_h()
return (board, solutionCount)