数独的“简单/朴素回溯强力算法”、“直接深度优先搜索”是众所周知并已实现的。
并且似乎不存在不同的实现。(当我第一次写这个问题时..我想说我们可以完全标准化它,但措辞很糟糕..)
我认为这个人很好地描述了该算法:/sf/answers/145284891/
编辑:所以让我用伪代码更详细地说明它......
var field[9][9]
set the givens in 'field'
if brute (first empty grid) = true then
output solution
else
output no solution
end if
function brute (cx, cy)
for n = 1 to 9
if (n doesn't present in row cy) and (n doesn't present in column cx) and (n doesn't present in block (cx div 3, cy div 3)) then
let field[cx][cy] = n
if (cx, cy) this is the last empty grid …Run Code Online (Sandbox Code Playgroud)