Erlang 中的回溯
首先对我的英语感到抱歉。
我想在 Erlang 中使用回溯算法。它将作为解决部分填充数独的猜测。 9x9 数独存储为 81 个元素的列表,其中每个元素存储可以进入该单元格的可能数字。
对于 4x4 数独,我最初的解决方案如下所示: [[1],[3],[2],[4],[4],[2],[3],[1],[2,3],[4],[1],[2, 3],[2,3],[1],[4],[2,3]]
此数独有 2 个解。我必须把它们都写出来。达到初始解决方案后,我需要实现回溯算法,但我不知道如何实现。
我的想法是将固定元素写到一个名为“fixedlist”的新列表中,这会将多解决方案单元格更改为 []。
对于上面提到的示例,固定列表如下所示: [[1],[3],[2],[4],[4],[2],[3],[1],[],[4],[1],[],[], [1],[4],[]]
从这里我有一个“样本”,我在解决方案列表中查找不等于 1 的最小长度,然后尝试该单元格的第一个可能的数字并将其放入到那个固定列表。这里我有一个算法来更新单元格并检查它是否仍然是可解的数独。如果没有,我不知道如何退一步尝试新的。 我知道它的伪代码,我可以将它用于命令式语言,但不能用于 erlang。 (prolog 实际上实现了回溯算法,但 erlang 没有)
有什么想法吗?
First of all sorry for my English.
I would like to use a backtracking algorithm in Erlang. It would serve as a guessing to solve partially filled sudokus. A 9x9 sudoku is stored as a list of 81 elements, where every element stores the possible number which can go into that cell.
For a 4x4 sudoku my initial solution looks like this:
[[1],[3],[2],[4],[4],[2],[3],[1],[2,3],[4],[1],[2,3],[2,3],[1],[4],[2,3]]
This sudoku has 2 solutions. I have to write out both of them. After that initial solution reached, I need to implement a backtracking algorithm, but I don't know how to make it.
My thought is to write out the fixed elements into a new list called fixedlist which will change the multiple-solution cells to [].
For the above mentioned example the fixedlist looks like this:
[[1],[3],[2],[4],[4],[2],[3],[1],[],[4],[1],[],[],[1],[4],[]]
From here I have a "sample", I look for the lowest length in the solutionlist which is not equal to 1, and I try the first possible number of this cell and I put it to that fixedlist. Here I have an algorithm to update the cells and checks if it is still a solvable sudoku or not. If not, I don't know how to step back one and try a new one.
I know the pseudo code of it and I can use it for imperative languages but not for erlang. (prolog actually implemented backtrack algorithm, but erlang didn't)
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回复:我的回溯功能。
这些是通用函数,它们提供了处理回溯和逻辑变量的框架,类似于序言引擎。您必须提供描述程序逻辑的函数(谓词)。如果你像在 prolog 中那样编写它们,我可以向你展示如何将它们翻译成 erlang。非常简单地,您将以下内容翻译为:
在序言中 为类似
此处我忽略除延续之外的所有其他参数。
我希望这能提供一些帮助。正如我所说,如果你描述你的算法,我可以帮助你翻译它们,我一直在寻找一个很好的例子。当然,您也可以自己做,但这提供了一些帮助。
Re: My bactracking functions.
These are the general functions which provide a framework for handling back-tracking and logical variables similar to a prolog engine. You must provide the function (predicates) which describe the program logic. If you write them as you would in prolog I can show you how to translate them into erlang. Very briefly you translate something like:
in prolog into something like
Here I am ignoring all other arguments except the continuations.
I hope this gives some help. As I said if you describe your algorithms I can help you translate them, I have been looking for a good example. You can, of course, just as well do it by yourself but this provides some help.