-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsudoku_solver.py
More file actions
54 lines (44 loc) · 1.45 KB
/
sudoku_solver.py
File metadata and controls
54 lines (44 loc) · 1.45 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
def validcellvalue(x, y, n, grid, size):
# Validate row and column.
for i in range(size):
if grid[x][i] == n or grid[i][y] == n:
return False
blockrows = 2
blockcols = 2
if size == 6:
blockcols += 1
if size == 9:
blockrows += 1
blockcols += 1
inix = (x // blockrows) * blockrows
iniy = (y // blockcols) * blockcols
# Validate block.
for i in range(blockrows):
for j in range(blockcols):
if grid[inix+i][iniy+j] == n:
return False
return True
def solveaux(grid, solutions):
size = len(grid)
# For each row.
for x in range(size):
# For each column.
for y in range(size):
# If target cell is empty.
if grid[x][y] is None:
# For each possible value the cell can take.
for n in range(1, size+1):
# If the value is valid.
if validcellvalue(x, y, n, grid, size):
grid[x][y] = n
# Process the remaining cells.
solveaux(grid, solutions)
# Backtrack.
grid[x][y] = None
return
solutions.add(tuple(tuple(row) for row in grid))
def solve(grid):
"""Returns a set with the solutions for a 4x4, a 6x6 or a 9x9 Sudoku."""
solutions = set()
solveaux(grid, solutions)
return solutions