最难数独的快速解法 – python

最难数独的快速解法 – python转载于 www jianshu com p 1b2ee6539 python3 numpy 的学习递归算法世界最难数独芬兰数学家因卡拉 花费 3 个月时间设计出了世界上迄今难度最大的数独游戏 而且它只有一个答案 因卡拉说只有思考能力最快 头脑最聪明的人才能激活成功教程这个游戏数独解法有很多 这里练习用排除 递归回溯法 排除法很直观根据已知的数字 排除同一行 同一列 同一九宫格内

转载于: www.jianshu.com/p/1b2ee6539…

  • python3、numpy的学习
  • 递归算法

世界最难数独

芬兰数学家因卡拉,花费3个月时间设计出了世界上迄今 难度最大的 数独游戏,而且它只有一个答案。因卡拉说只有思考能力最快、头脑最聪明的人才能激活成功教程这个游戏

数独解法

有很多,这里练习用排除+递归回溯法。

  1. 排除法很直观
  • 根据已知的数字,排除同一行、同一列、同一九宫格内相同的数字
  • 同一九宫格内,如果存在某一行/列上猜测有两个同一数字,那大数独同一行/列也能排除
  • 新解出的数字,加入到先进先出队列(栈) – FIFO Queue
  1. 猜测+回溯法
  • 如果已经没有任何已知的数字,那就只能猜测了
  • 把猜测数字加入到后进先出(LastInFirstOut)队列 – LIFO Queue
  • 递归写法,画流程图会比较容易理解!!

根据流程图写代码,会很轻松,逻辑也不会乱:

 def sudo_solve_iter(self): # 排除法解题 self.sudo_exclude() # logger.debug(f'excluded, current result:\n{self.value}') if self.verify_value(): if self.get_num_count() == 81: # solve success return else: logger.info(f'current no. of fixed answers: {self.get_num_count()}') point = self.get_best_point() index = 0 items = self.add_to_queue(point, index) logger.info(f'add to LIFO queue and guessing {items[index]}/{items}: ' f'{[x.point for x in self.record_queue.queue]}') self.guess_times += 1 return self.sudo_solve_iter() while True: if self.record_queue.empty(): # raise Exception('Sudo is wrong, no answer!') logger.error(f'Guessed {self.guess_times} times. Sudo is wrong, no answer!') exit() # check value ERROR, need to try next index or rollback record = self.record_queue.get() point = record.point index = record.point_index + 1 items = record.value[point] self.value = record.value logger.info(f'Recall! Pop previous point, {items} @{point}') # 判断索引是否超出范围 # if not exceed,则再回溯一次 if index < len(items): items = self.add_to_queue(point, index) logger.info(f'guessing next index: answer={items[index]}/{items} @{point}') self.guess_times += 1 return self.sudo_solve_iter() 复制代码

实战

最难数独,需要猜测109次?!确实很变态啊!不过就算i3级别的旧电脑,也只要0.3s左右。

/home/kevin/git/sudo-py3/venv/bin/python /home/kevin/git/sudo-py3/sudo-recur.py DEBUG:__main__:current no. of fixed answers: 21 DEBUG:__main__:add to LIFO queue and guessing 3/[3, 9]: [(7, 6)] DEBUG:__main__:current no. of fixed answers: 22 DEBUG:__main__:add to LIFO queue and guessing 5/[5, 9]: [(7, 6), (6, 6)] DEBUG:__main__:current no. of fixed answers: 25 DEBUG:__main__:add to LIFO queue and guessing 6/[6, 8, 9]: [(7, 6), (6, 6), (5, 6)] DEBUG:__main__:verify failed. dup in col 0 DEBUG:__main__:Recall! Pop previous point, [6, 8, 9] @(5, 6) DEBUG:__main__:guessing next index: answer=8/[6, 8, 9] @(5, 6) DEBUG:__main__:current no. of fixed answers: 29 DEBUG:__main__:add to LIFO queue and guessing 2/[2, 4, 6]: [(7, 6), (6, 6), (5, 6), (5, 1)] DEBUG:__main__:verify failed. dup in col 0 ... DEBUG:__main__:guessing next index: answer=3/[2, 3, 8] @(4, 3) DEBUG:__main__:current no. of fixed answers: 41 DEBUG:__main__:add to LIFO queue and guessing 1/[1, 4]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (1, 1)] DEBUG:__main__:verify failed. dup in row 0 DEBUG:__main__:Recall! Pop previous point, [1, 4] @(1, 1) DEBUG:__main__:guessing next index: answer=4/[1, 4] @(1, 1) DEBUG:__main__:current no. of fixed answers: 42 DEBUG:__main__:add to LIFO queue and guessing 1/[1, 6, 8]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (1, 1), (4, 1)] DEBUG:__main__:verify failed. dup in row 4 DEBUG:__main__:Recall! Pop previous point, [1, 6, 8] @(4, 1) DEBUG:__main__:guessing next index: answer=6/[1, 6, 8] @(4, 1) DEBUG:__main__:verify failed. dup in row 0 DEBUG:__main__:Recall! Pop previous point, [1, 6, 8] @(4, 1) DEBUG:__main__:guessing next index: answer=8/[1, 6, 8] @(4, 1) DEBUG:__main__:verify failed. dup in row 0 DEBUG:__main__:Recall! Pop previous point, [1, 6, 8] @(4, 1) DEBUG:__main__:Recall! Pop previous point, [1, 4] @(1, 1) DEBUG:__main__:Recall! Pop previous point, [2, 3, 8] @(4, 3) DEBUG:__main__:guessing next index: answer=8/[2, 3, 8] @(4, 3) DEBUG:__main__:current no. of fixed answers: 33 DEBUG:__main__:add to LIFO queue and guessing 2/[2, 3]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (3, 3)] DEBUG:__main__:current no. of fixed answers: 42 DEBUG:__main__:add to LIFO queue and guessing 3/[3, 4]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (3, 3), (7, 1)] DEBUG:__main__:current no. of fixed answers: 45 DEBUG:__main__:add to LIFO queue and guessing 1/[1, 6]: [(7, 6), (6, 6), (6, 1), (6, 3), (4, 3), (3, 3), (7, 1), (4, 1)] DEBUG:__main__:verify failed. dup in row 0 DEBUG:__main__:Recall! Pop previous point, [1, 6] @(4, 1) DEBUG:__main__:guessing next index: answer=6/[1, 6] @(4, 1) INFO:__main__:Done! guessed 109 times, in 0.540sec INFO:__main__:Puzzle: [[8 0 0 0 0 0 0 0 0] [0 0 3 6 0 0 0 0 0] [0 7 0 0 9 0 2 0 0] [0 5 0 0 0 7 0 0 0] [0 0 0 0 4 5 7 0 0] [0 0 0 1 0 0 0 3 0] [0 0 1 0 0 0 0 6 8] [0 0 8 5 0 0 0 1 0] [0 9 0 0 0 0 4 0 0]] INFO:__main__:Answer: [[8 1 2 7 5 3 6 4 9] [9 4 3 6 8 2 1 7 5] [6 7 5 4 9 1 2 8 3] [1 5 4 2 3 7 8 9 6] [3 6 9 8 4 5 7 2 1] [2 8 7 1 6 9 5 3 4] [5 2 1 9 7 4 3 6 8] [4 3 8 5 2 6 9 1 7] [7 9 6 3 1 8 4 5 2]] 复制代码

源码:github.com/kevinnj/s…

参考:Python秒解最难数独 – 杨仕航的博客 http://yshblog.com/blog/74

预告1:会写一个小网站,秒解任何你输入的数独题目 mysudo.herokuapp.com/

  • 预告2:添加扫一扫功能,人工智能识别拍摄的数独题目,Opencv抓图 + CNN卷积网络识别。
  • 预告3:集成到微信小程序
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/212252.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午8:28
下一篇 2026年3月18日 下午8:28


相关推荐

  • 数学建模方法——皮尔逊相关系数及其显著性检验 (Pearson correlation coefficient)

    数学建模方法——皮尔逊相关系数及其显著性检验 (Pearson correlation coefficient)0 皮尔逊相关系数简介相关系数是衡量两个数据相关关系的指标 两个数据相关在某种程度上可以帮助人们理解事物的变化规律 例如在商品推荐中 我们已知一个用户 A 的购买喜好 同时发现另一个用户 B 的购买数据和 A 相关性很高 那么我们可以根据 A 的喜好去给 B 推荐相关的产品 等等 皮尔逊相关系数 Pearsoncorre 就是最为常用的用来衡量两个变量线性相关关系的指标 有了

    2026年3月20日
    2
  • Unified Functional Testing12.02(UFT)安装教程

    Unified Functional Testing12.02(UFT)安装教程UnifiedFunctionalTesting12.02安装教程相关说明​ UnifiedFunctionalTesting(UTF)是QuickTestProfessional(QTP)11.5版本以后的名称实验证明教程中的《安装UnifiedFunctionalTesting的Update》跳过也行,安装完MicrosoftScriptDebugger之后就可以…

    2022年5月8日
    187
  • 1,Linux vim的常用快捷键

    1,Linux vim的常用快捷键1,Linux/vim的常用快捷键1,移动HJKL.H:向左L:向右J:向下K:向上e:跳跃到单词末尾b:跳跃到单词首字母w:跳跃到下一个单词的首字母shift+6:跳跃到本行的开头shift+$:跳跃到本行的末尾2,翻页Ctrl+F:向下一页Ctrl+B:向上一页Ctrl+E:向下(符合视觉)Ctrl+Y:向上shift+g:翻到文件末尾gg:翻到文件开头3,其它i:光标位置

    2022年6月2日
    41
  • VUE集成Office插件NTKO

    VUE集成Office插件NTKO使用 NTKO 插件 这玩意能不用就别用 因为它是插件 局限性比较大 在无法预知的用户环境上 各种问题 但是在安全上做的比较好 不会在用户本地缓存临时文件等 都是在内存中操作 然后流传输 over 浏览器判断 computed browser constuserAge navigator userAgent constrMsie msie s trident rv w constrFir

    2026年3月17日
    1
  • 小白能读懂的 《手把手教你学DSP(TMS320X281X)》第四章 2020-12-29 完整工程「建议收藏」

    小白能读懂的 《手把手教你学DSP(TMS320X281X)》第四章 2020-12-29 完整工程「建议收藏」4.1综述projects->include文件夹下有很多.h结尾的文件,是dsp的头文件,定义了dsp2812的一些数据结构,TI公司给的,无需修改。projects->Libraries文件下.lib后缀的是库文件。projects->Source文件下.c后缀的是源文件,平时写的代码放在这;最后的.cmd文件叫做cmd文件,为代码和数据分配存储空间。所以,完整工程=头文件+库文件+源文件+cmd文件4.2具体叙述…

    2022年6月6日
    32
  • 15个网站性能测试工具有哪些_国产性能测试工具

    15个网站性能测试工具有哪些_国产性能测试工具网站的加载速度是决定网站等级的重要因素,值得站长特别关注。原因很简单,没有人愿意为了打开一个网页而等老半天,换句话说,如果你的网站打开速度很慢,将流失大量的访客,甚至出现多米诺效应的不良影响。在埋头深

    2022年8月5日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号