数独生成器算法

发布于 01-05 20:30 字数 1436 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

也只是曾经2025-01-12 20:30:37

您看过现有的算法和/或代码吗?

查看https://www.sudokuwiki.org/Sudoku_Creation_and_Grading.pdf了解算法描述,以及 Peter Norvig 的文章 http://norvig.com/sudoku.html

Python 中有一些实现。到目前为止,我从未见过已发布的 C# 解决方案。

Have you looked at existing algorithms and/or code?

Check out https://www.sudokuwiki.org/Sudoku_Creation_and_Grading.pdf for an algorithmic description, and Peter Norvig's article at http://norvig.com/sudoku.html.

There are some implementations out there in Python. So far I've never seen a published C# solution.

软糖2025-01-12 20:30:37

如果您正在研究一些现有的算法,那么可以使用 ac# 项目。这实际上来自与 Peter Norvig 相同的解决方案。 此处了解更多信息

希望这会有所帮助!

If you are looking at some existing algorithms then there are a c# project for that. That acually comes from the same solution as Peter Norvig's. Read more about it here

Hope this will help!

驱逐舰岛风号2025-01-12 20:30:37

我使用编程来删除与最后一个条目冲突的所有先前条目。通过这种方法,我可以输入一些给定并定期计算解决方案或输入整个网格。我没有使用整个网格的随机条目,因为随机删除先前的条目可能会导致较长的设置。对于随机条目,我预计阻止冲突条目会导致空白单元格,因此答案可能是接受较长的设置,同时删除先前的冲突条目。您将需要返回所有空单元格,直到没有空单元格剩余。当所有单元格都被填满时,解决方案必须是有效的,否则冲突就会被消除。

I use programming to remove all prior entries that conflict with the last entry. With this method, I can enter some givens and periodically take a count of the solutions or enter the entire grid. I have not used random entry of the entire grid because random removal of prior entries could result in a long setup. For random entry, I would expect that blocking entry of a conflict would lead to a blank cell, so the answer may be to live with a long setup while removing prior conflicting entries. You will need to return to all empty cells until no empty cell remains. When all cells are filled, the solution must be valid, otherwise a conflict would have been removed.

若能看破又如何2025-01-12 20:30:37

我的方法是将您的第一种方法和第二种方法结合在一起。

首先,您必须有一个数独求解器。将求解器应用于空数独。那就是在没有线索的情况下寻找数独的解法。按从左上到右下的顺序填写数字。不然就有时间问题了。我不知道为什么。不管怎样,它的工作速度非常快,无需等待即可完成数独谜题。当您应用回溯时,请打乱每个位置的可能数字列表。否则,你每次都会遇到相同的难题。

其次,随机化所有职位的新列表。这是一个按随机顺序排列的 81 个位置的列表。根据这个顺序列表,尝试从上面的谜题中删除数字。每次删除一个数字时,您都必须检查它是否有多个解决方案。如果它有不止一种解决方案。该数字应放回并尝试随机列表中的下一个位置。此过程一直持续到列表末尾或者您已成功从拼图中删除 64 个数字。这个数字是64,因为有人证明了不存在少于17条线索且有唯一解的数独。此过程持续 15 秒到 2 分钟不等。通常 30 秒即可完成一个数独谜题。

第三,如果您不想为每个数独谜题等待 30 秒到 2 分钟,您可以对上述数独应用一些突变。这包括切换行和列、旋转。您还可以重新映射数字。例如,1→2、2→3...9→1。经过旋转和重新映射后,没有人会注意到这是原始的数独。

My approach is combining your first and second methods together.

First, you must have a sudoku solver. Apply the solver on an empty sudoku. That is to find a solution for a sudoku with no clues. Filling in numbers from top left to right bottom in order. Otherwise, there is a time issue. I don't know why. Anyway, it works very fast wait no time to have a completed sudoku puzzle. When you apply backtracking, shuffle the list of possible numbers for each position. Otherwise, you will get the same puzzle everytime.

Second, randomize a new list of all positions. That is a list of 81 positions in random order. According to this list of order, try removing numbers from the above puzzle. Everytime, you remove a number, you have to check if it has more than one solution. If it has more than one solution. The number should put back and try next position in the random list. This process continues until the end of list or you already has removed 64 numbers from the puzzle successfully. The number is 64 because somebody has proved that there is no sudoku with less than 17 clues with unique solution. This process is varies from 15 seconds to 2 minutes. Usually 30 seconds to get a sudoku puzzle.

Third, if you don't want to wait 30 seconds to 2 minutes for each sudoku puzzle, you may apply some mutations to the above sudoku. This includes switching rows and columns, rotating. You may also remap the numbers. For example, 1->2, 2->3...9->1. After rotating and remapping, no one will notice this is the original sudoku.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文