我正在使用SML / NJ创建一个Sudoku Solver。我已经有了所有的功能来实际操作输入数据(检查行的合法性,强制空格等)但我在回溯部分遇到了麻烦。
我遇到this question,但我对如何在SML中实现它感到困惑。
请注意,电路板作为列表列表输入,表示每行中的数字,0表示未知点
[[0,0,0, 2,6,0, 7,0,1],
[6,8,0, 0,7,0, 0,9,0],
[1,9,0, 0,0,4, 5,0,0],
[8,2,0, 1,0,0, 0,4,0],
[0,0,4, 6,0,2, 9,0,0],
[0,5,0, 0,0,3, 0,2,8],
[0,0,9, 3,0,0, 0,7,4],
[0,4,0, 0,5,0, 0,3,6],
[7,0,3, 0,1,8, 0,0,0]]
这是我的(编辑过的)solve
功能。
exception Sudoku;
fun solve board =
let fun solve' board k =
(* k is the row we are working on, so if it's 9, we SHOULD be done *)
if k = 9 then board else
let
(* get row k from the board *)
val row = (List.nth (board, k));
fun trySpot number col =
(* if number >= 10, raise an exception to backtrack *)
if number > (length row) then raise Sudoku
(* if col = 9, raise an exception to backtrack *)
else if col = 9 then raise Sudoku
(* if row[col] is not a zero, move to next col *)
else if not (List.nth(row, col) = 0) then trySpot number (col + 1)
(* row doesn't contain this num already *)
else if length (List.filter (fn x => x = number) row) = 0 then
let
(* build the new row and board and check if legal (this works fine) *)
val newRow = delete(col + 1, (insertAtPos row number col));
val newBoard = delete(k + 1, (insertAtPos board newRow k));
val isLegal = checkLegal newBoard;
in
(* if legal, keep solving with new board as base *)
if isLegal then
solve' (force newBoard) 0
handle Sudoku => solve' (force board) (k + 1)
(* not legal, try with next num *)
else trySpot (number + 1) col
end
(* row already has this number, skipping *)
else trySpot (number + 1) col
in
(* if board is complete and legal we're done *)
if completedBoard board andalso checkLegal board then board
(* if row has a zero then try a spot *)
else if (zeroInList row) then trySpot 1 0
(* otherwise move to next row *)
else solve' (force board) (k + 1)
end
in
(* initial solve *)
solve' (force board) 0
end;
在上面的示例数据上调用solve
会返回以下列表
[[4,3,5,2,6,9,7,8,1],
[6,8,2,5,7,1,4,9,3],
[1,9,7,8,3,4,5,6,2],
[8,2,6,1,9,5,3,4,7],
[3,1,4,6,8,2,9,0,0],
[0,5,0,0,0,3,0,2,8],
[0,0,9,3,0,0,0,7,4],
[2,4,0,0,5,0,0,3,6],
[7,0,3,0,1,8,0,0,0]]
现在这部分是正确的。基于我曾经检查的在线数独求解器,前四行看起来是完全正确的,但它在第5行搞砸了。我认为这是因为它无法一直回溯。
它“备份”的唯一地方是在这一行
handle Sudoku => solve' (force board) (k + 1)
这告诉它只是尝试解决旧电路板(没有新号码),但是这样可以防止它回溯多个步骤(我认为)。怎么能实现呢?
如果有人想要查看完整代码,您可以找到它here.
提前致谢!
答案 0 :(得分:1)
我似乎已经使用我的求解功能来处理我尝试过的所有情况。诀窍是跟踪我们正在处理的现场非法数字列表。解算器现在将跳过这些数字,如果无法找到前进路径,则回溯。
exception Sudoku;
(* solves a soduku board input that gives a list of lists representing the rows of a board (0 for unknown spaces) *)
fun solve board =
let fun solve' board row illegalNums =
(* if we are on row 9 and board is complete/legal, we are done *)
if row = 9 andalso completedBoard board andalso checkLegal board then board else
(* if we are on row 9 but the board is not complete/legal, throw an exception to backtrack *)
if row = 9 then raise Sudoku else
let
(* get the current row we are working on *)
val cRow = (List.nth (board, row));
(* trys a row[col] on the board with a certain number *)
fun trySpot num cRow col =
(* if number >= 10, raise an exception to backtrack *)
if num > 9 then raise Sudoku
(* if row[col] is not a 0, try next col *)
else if not (List.nth (cRow, col) = 0) then trySpot num cRow (col + 1)
(* if we know that the number we are on isn't allowed, skip to next number *)
else if length (List.filter (fn x=> x = num) illegalNums) > 0 then trySpot (num + 1) cRow col
(* if row already has this number, skip to next number *)
else if length (List.filter (fn x=> x = num) cRow) > 0 then trySpot (num + 1) cRow col
(* if col already has this number, skip to next number *)
else if length (List.filter (fn x=> x = num) (List.nth((rowsToCols board), col))) > 0 then trySpot (num + 1) cRow col
else
let
(* make our new row and board *)
val newRow = delete(col + 1, (insertAtPos cRow num col));
val newBoard = delete(row + 1, (insertAtPos board newRow row));
in
(* if new board is legal, continue solving *)
if checkLegal newBoard then
solve' (force newBoard) 0 []
(* handle any exceptions by adding the number to our list of illegal numbers, thereby backtracking *)
handle Sudoku => solve' (force board) row (illegalNums@[num])
(* if new board is not legal, try next number *)
else trySpot (num + 1) cRow col
end
in
(* if board is completed and legal, return board *)
if completedBoard board andalso checkLegal board then board
(* if current row has at least one 0, try a number in that row (beginning at col 1) *)
else if (zeroInList cRow) then trySpot 1 cRow 0
(* else, skip to next row and solve *)
else solve' (force board) (row + 1) []
end
in
(* initial solve *)
solve' (force board) 0 []
end;
较难的电路板可能需要相当长的时间才能完全解决,但我测试的每一个电路板最终都会到达那里。我确信我的代码中可以做很多优化,所以我愿意接受建议。