Package sage :: Package games :: Module sudoku
[hide private]
[frames] | no frames]

Module sudoku

source code


Sudoku Solver

Given a 9x9 Sudoku puzzle as an integer matrix, the program solves it.



Functions [hide private]
 
sudoku(A)
Solve the 9x9 Sudoku puzzle defined by the matrix $A$.
source code
 
grid_has_k(A, i, j, k)
Return \code{True} precisely if the 3x3 submatrix that contains the $i$,$j$ position of $A$ already has a $k$ in it.
source code
 
solve_recursive(A, i, j) source code
Variables [hide private]
  copies = 0
  R9 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
  R10 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Function Details [hide private]

sudoku(A)

source code 

Solve the 9x9 Sudoku puzzle defined by the matrix $A$.

EXAMPLE:
    sage: A = matrix(ZZ,9,[5,0,0, 0,8,0, 0,4,9, 0,0,0, 5,0,0, 0,3,0, 0,6,7, 3,0,0, 0,0,1,  1,5,0, 0,0,0, 0,0,0,  0,0,0, 2,0,8, 0,0,0,    0,0,0, 0,0,0, 0,1,8,     7,0,0, 0,0,4, 1,5,0,   0,3,0, 0,0,2, 0,0,0,  4,9,0, 0,5,0, 0,0,3])
    sage: A
    [5 0 0 0 8 0 0 4 9]
    [0 0 0 5 0 0 0 3 0]
    [0 6 7 3 0 0 0 0 1]
    [1 5 0 0 0 0 0 0 0]
    [0 0 0 2 0 8 0 0 0]
    [0 0 0 0 0 0 0 1 8]
    [7 0 0 0 0 4 1 5 0]
    [0 3 0 0 0 2 0 0 0]
    [4 9 0 0 5 0 0 0 3]
    sage: sudoku(A)
    [5 1 3 6 8 7 2 4 9]
    [8 4 9 5 2 1 6 3 7]
    [2 6 7 3 4 9 5 8 1]
    [1 5 8 4 6 3 9 7 2]
    [9 7 4 2 1 8 3 6 5]
    [3 2 6 7 9 5 4 1 8]
    [7 8 2 9 3 4 1 5 6]
    [6 3 5 1 7 2 8 9 4]
    [4 9 1 8 5 6 7 2 3]