Anonymous user
Sudoku: Difference between revisions
→{{header|Ruby}}
(add Ruby) |
|||
Line 600:
=={{header|Ruby}}==
Example of a back-tracking solver, from [[wp:Algorithmics of sudoku]]
{{works with|Ruby|1.8.7+}}
<lang ruby>def read_matrix(fh)
matrix = []
Line 652 ⟶ 653:
def solve_sudoku(matrix)
options = []
(0..8).each { |i|
Line 659 ⟶ 660:
p = permissible(matrix, i, j)
# If nothing is permissible, there is no solution at this level.
return
options.push({:i => i, :j => j, :permissible => p})
}
Line 666 ⟶ 667:
return matrix if options.length == 0
omin = options.
# If there is an option with only one solution, set it and re-check permissibility
Line 681 ⟶ 680:
mtmp[omin[:i]][omin[:j]] = v
ret = solve_sudoku(mtmp)
}
# We did an exhaustive search on this branch and nothing worked out.
return
end
end
def print_matrix(matrix)
if
return
end
|