带回溯的SML数独求解器
我正在用SML/NJ创建一个数独解算器。我已经准备好了所有的函数来实际操作输入数据(检查行的合法性,强制使用空格等),但是回溯部分遇到了问题 我遇到了一些问题,但我对如何在SML中实现它感到困惑 请注意,电路板是以列表的形式输入的,表示每行中的数字,0表示未知点带回溯的SML数独求解器,sml,sudoku,backtracking,smlnj,Sml,Sudoku,Backtracking,Smlnj,我正在用SML/NJ创建一个数独解算器。我已经准备好了所有的函数来实际操作输入数据(检查行的合法性,强制使用空格等),但是回溯部分遇到了问题 我遇到了一些问题,但我对如何在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,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]]
现在这部分是正确的。根据我以前检查的在线数独解算器,前四行看起来是完全正确的,但第五行就搞乱了。我想这是因为它不能一路后退
它“备份”的唯一位置是在这条线上
handle Sudoku => solve' (force board) (k + 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;
较难的电路板可能需要相当长的时间才能完全解决,但我测试过的每一块电路板最终都能解决。我确信在我的代码中可以进行大量优化,因此我愿意接受建议。Hansen和Rischel的《使用SML编程入门》一书中包含了一个回溯算法的示例,该算法可以解决使用异常实现的8皇后问题。我能够修改他们的代码,不费吹灰之力就能获得骑士之旅。它可能会给你一些想法(尽管现在我更倾向于使用选项而不是例外)look@JohnColeman,我查看了它们的实现,并尝试修改我的solve函数来实现异常的回溯。它在某种程度上是有效的,但我不确定如何使它进一步回到决策树中。有什么想法吗?现在没有想法,我从来没有做过很多数独游戏(甚至不是手工操作——我更喜欢填字游戏和密码)。我只是觉得这本书会给你一些想法。如果本周晚些时候我有时间的话,我会考虑一下,但是接下来的几天我很忙。我很高兴你让它工作起来。也许您可以将其发布在代码审查上,以获得更多反馈。我认为,一个改进是摒弃例外,转而采用选项。返回
NONE
将替换引发异常和针对NONE
的模式匹配,替换handle
。我读到,在O'CAML中,至少使用选项比引发异常要快得多,因为它不涉及展开调用堆栈。请看这个问题:再仔细阅读一下,我不确定使用选项是否会有时间上的收获,尽管我仍然认为选项在概念上比例外更清晰。有一件事可能会有所不同,那就是用vectors
替换列表-对于您的电路板表示,您的电路板具有固定的大小,因此您不需要列表的增长能力,而且向量的O(1)
访问时间可能会有所增加。@Johncolman感谢您的建议。我将查看选项,而不是异常。不幸的是,我被清单困住了。这是一个课堂作业,我们的教授希望我们使用简单的旧列表。我可能还是会胡闹,看看向量是否有改进,所以谢谢你的想法!听起来你有个好教授。真正学习一门语言的唯一方法就是把它用于那些既不重要又有趣的问题。