创建数独初始板
有没有一种算法或方法可以让我获得数独游戏的初始状态数独谜题。最好有不同难度级别的能力?
Is there an algorithm or way I can get initial state sudoku puzzles for a sudoku game. Preferably with the ability to have different levels of difficulty?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能对此数独生成器感兴趣。
You might be interested in this Sudoku generator.
我最近开源了我的 Objective C 数独板生成器,如果您有兴趣...
http://jayfuerstenberg.com/devblog/open-source-code-for-developing-sudoku-for-iphone-and-os-x
显然是制作的适用于 iPhone 和 Mac,但逻辑可以移植到您正在开发的任何平台。
I recently open sourced my Objective C Sudoku board generator, if you're interested...
http://jayfuerstenberg.com/devblog/open-source-code-for-developing-sudoku-for-iphone-and-os-x
It's obviously made for iPhone and the Mac but the logic is portable to whatever platform you happen to be developing for.
我相信 Devin 正在寻找一个初始数独配置,或者一个数独谜题(填充的单元格少于 81 个),它应该保证存在 1 个或多个解决方案。随机的 N 个单元配置可能无法保证解的存在。
我想到的方法是首先获得一个完整的数独解决方案,将其用作基础(将其命名为X)
通过按任意顺序应用任意数量的以下变换,X 可以变换为大量其他有效的数独解 X1、X2、X3:
一个。旋转
b.镜面翻转
c.将所有数字 x 与数字 y 交换。
然后,通过从基数中随机扣除单元格,每个基数都可以用于生成数独谜题。
I believe Devin is looking for a initial sudoku configuration, or a sudoku puzzle (with less than 81 cells filled) which should guarantee 1 or more solution exists. A random N cell configuration may not guarantee a solution exists.
The way I think of is first obtain a full sudoku solution, using it as a base (name it X)
X can be transformed into large amount of other valid sudoku solutions, X1, X2, X3, by applying any number of following transformations in any sequence:
a. rotation
b. mirror flip
c. swap all number x with number y.
Each of these bases then can be used to generate your sudoku puzzle, by randomly deducting cells from the base.
在 Scala 中玩得很开心。您可以删除更多单元格以使其变得更加困难。Scala
Had some fun with it in Scala. You can remove more cells to make it more difficult.Scala
一种获取 9x9 数独元素的递归方法。
A recursively way to get 9x9 sudoku elements.
基本上有两种方法。在这两种情况下,您都需要有 2 个求解器,一个类似人类的求解器,它使用可由人类和回溯求解器。
使用第一种方法,您可以生成随机完整解决方案并迭代删除随机单元解决方案。回溯求解器将确保仍然只存在一种解决方案,而类人求解器将确保它仍然可以由人类解决,并且还可以用于衡量难题的难度。
第二种方法的工作方式相反。首先,您创建一个空板并随机放置 17 个细胞溶液(以一致的方式)。 17 是已知可生成具有独特解决方案的谜题的最低填充单元数。现在,每个步骤中的算法都会检查是否已经有唯一的解决方案,如果没有,则添加另一个(持续)填充的单元格。如果解决方案保证解决方案的唯一性并且难题可由人类解决并且难度低于某个限制,则算法终止。
Basically there are two approaches. In both you need to have 2 solvers, a humanlike solver, which uses strategies performable by a human and a backtracking solver.
With the first approach you generate a random complete solution and iteratively remove random cells solutions. Backtracking solver will make sure, that there still exist only one solution, while the human-like solver will make sure, that its still solvable by human and it can be also used to measure the difficulty of the puzzle.
The second approach works in an opposite fashion. Firstly you create an empty board and place there randomly 17 cell solutions (in a consistent manner). 17 is the lowest filled cell count known to genrate a puzzle with unique solution. Now the algorithm in every step checks, if it has already an unique solution and if not, it adds another (consitently) filled cell. If the solution guarantees solution uniquesness and the puzzle is solvable by a human and the difficulty is below some limit, than the algorithm terminates.
http://www.sudokuwiki.org/Sudoku_Creation_and_Grading.pdf
http://www.sudokuwiki.org/Sudoku_Creation_and_Grading.pdf