Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (48 sloc) 1.64 KB
"""Sudoku solver."""
from ortools.sat.python import cp_model
def solve_sudoku():
"""Solves the sudoku problem with the CP-SAT solver."""
# Create the model.
model = cp_model.CpModel()
cell_size = 3
line_size = cell_size**2
line = range(line_size)
cell = range(cell_size)
initial_grid = [
[0, 6, 0, 0, 5, 0, 0, 2, 0], [0, 0, 0, 3, 0, 0, 0, 9, 0],
[7, 0, 0, 6, 0, 0, 0, 1, 0], [0, 0, 6, 0, 3, 0, 4, 0, 0],
[0, 0, 4, 0, 7, 0, 1, 0, 0], [0, 0, 5, 0, 9, 0, 8, 0, 0],
[0, 4, 0, 0, 0, 1, 0, 0, 6], [0, 3, 0, 0, 0, 8, 0, 0, 0],
[0, 2, 0, 0, 4, 0, 0, 5, 0]
]
grid = {
(i, j): model.NewIntVar(1, line_size, 'grid %i %i' % (i, j))
for i in line for j in line
}
# AllDifferent on rows.
for i in line:
model.AddAllDifferent([grid[(i, j)] for j in line])
# AllDifferent on columns.
for j in line:
model.AddAllDifferent([grid[(i, j)] for i in line])
# AllDifferent on cells.
for i in cell:
for j in cell:
one_cell = [
grid[(i * cell_size + di, j * cell_size + dj)] for di in cell
for dj in cell
]
model.AddAllDifferent(one_cell)
# Initial values.
for i in line:
for j in line:
if initial_grid[i][j]:
model.Add(grid[(i, j)] == initial_grid[i][j])
# Solve and print out the solution.
solver = cp_model.CpSolver()
status = solver.Solve(model)
if status == cp_model.FEASIBLE:
for i in line:
print([int(solver.Value(grid[(i, j)])) for j in line])
if __name__ == '__main__':
solve_sudoku()
You can’t perform that action at this time.