转载于: 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 successreturnelse:logger.info(f'current no. of fixed answers: {self.get_num_count()}')point = self.get_best_point()index = 0items = 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 += 1return 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 rollbackrecord = self.record_queue.get()point = record.pointindex = record.point_index + 1items = record.value[point]self.value = record.valuelogger.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 += 1return self.sudo_solve_iter()
复制代码

实战

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

/home/kevinqq/git/sudo-py3/venv/bin/python /home/kevinqq/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/kevinqqnj/s…

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

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

  • 预告2:添加扫一扫功能,人工智能识别拍摄的数独题目,Opencv抓图 + CNN卷积网络识别。
  • 预告3:集成到微信小程序

最难数独的快速解法 - python相关推荐

  1. Python来处理数独游戏(含世界最难数独示例)

    数独(sudoku)是一种填数字的游戏,在一个9*9的九宫格里面推导出剩余的数字,要求每行.每列.每宫(3*3)的数字均包含1~9,且不重复! 另:世界最难数独(题目中的最后一个数独)被江苏扬州一位农 ...

  2. 算法实践:数独的基本解法

    数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复. 每一道合格的数独谜 ...

  3. 算法实践——数独的基本解法

    数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复. 每一道合格的数独谜 ...

  4. 如何自学python爬虫-小白如何快速学习Python爬虫?

    原标题:小白如何快速学习Python爬虫? 很多同学想学习 爬虫 ,对于小白来说,爬虫可能是一件非常复杂.技术门槛很高的事情.而且爬虫是入门 Python 最好的方式,没有之一. 我们可以通过爬虫获取 ...

  5. 想学python编程-【经验分享】新手如何快速学好Python?

    原标题:[经验分享]新手如何快速学好Python? 什么样的人适合学习编程?到底Python作为编程入门语言合适吗?学习Python编程,我们应该如何学?大概的学习内容包括哪些? 今天我们就用一篇文章 ...

  6. python怎么学最快-怎么快速自学python

    本文跟大家谈谈为什么要学python以及如何学好python. 一.作为初学者,应该如何学python? 很多人对python缩进试的简洁表达不以为然.那些都是已混迹于C和JAVA的老鸟已经习惯了花括 ...

  7. python编程入门指南-最简单的Python编程入门指南,没基础也能快速入门Python编程...

    原标题:最简单的Python编程入门指南,没基础也能快速入门Python编程 对Python这门编程语言来讲,几乎是没什么不能做到的.最难的不过是如何入门,也就是你进入Python编程的第一步. 其实 ...

  8. python 计算机程序设计-某高校计算机编程教授教你如何快速入门python,一文带你进入编程...

    image 如何快速入门Python 学习任何一门语言都是从入门(1年左右),通过不间断练习达到熟练水准(3到5年),少数人最终能精通语言,成为执牛耳者,他们是金字塔的最顶层.虽然万事开头难,但好的开 ...

  9. 学会python爬虫能发财么_python如何赚钱? python爬虫如何进阶? python就业? 如何快速入门python?...

    1.如何快速入门 Python ? 我之前给大家说过,速成一门技能是不可能的,你需要花很多时间才能真正的掌握一门技能,但是快速入门是有可能的,而且也是必要的,你需要掌握最少且最必要的知识点,先进门再说 ...

  10. 视频教程-快速入门Python基础教程_Python基础知识大全-Python

    快速入门Python基础教程_Python基础知识大全 十余年计算机技术领域从业经验,在中国电信.盛大游戏等多家五百强企业任职技术开发指导顾问,国内IT技术发展奠基人之一. 杨千锋 ¥99.00 立即 ...

最新文章

  1. linux 配置计算机和用户免密在本地计算机执行远程命令 hosts.equiv $HOME/.rhosts 简介
  2. Win7 Ubuntu13.04互通(win7下用vbox安装Ubuntu)
  3. ofbiz mysql_ofbiz+mysql安装求教
  4. PowerDesigner(一)-PowerDesigner概述(系统分析与建模)
  5. centos网络隔一段时间就断_潮汕青年说 | 瞎吃小哥:隔着屏幕也要把你看馋的顶配版吃货...
  6. 看我如何利用教科书级别的释放后使用漏洞(CVE-2020-6449)
  7. SparkSQL默认存储格式入门
  8. 基于百度通用翻译API的一个翻译小工具
  9. 软件测试师网络工程师,【软件测试工程师(华为项目)网络工程师面试题目|面试经验】-看准网...
  10. 【python】Python学到什么程度可以面试工作
  11. 带你初步了解基因表达调控
  12. nn.Flatten()函数详解及示例
  13. odoo14学习速记笔记
  14. Android中Bitmap的分析与使用
  15. 一款支持大恒相机、IDS相机、普通USB相机和机械手的标定工具
  16. 如何使用Fork版本工具下载分支内容
  17. 上海计算机考试分值,2019年上海中考总分是多少 考试科目及分值
  18. 京东联盟开发(6)——推广链接解析SKUID
  19. JSP SSH机械设备台账管理系统myeclipse开发mysql数据库MVC模式java编程网页设计
  20. xxl-job基本使用

热门文章

  1. Appium 1.21.x 百度网盘下载
  2. 2020年“华为杯”中国研究生数学建模竞赛题目及简单分析
  3. 正点原子STM32F429阿波罗板,固件库工程搭建
  4. python安装win32com模块
  5. matlab人工鱼群捕食,Matlab从入门到精通(3)——多目标人工鱼群算法
  6. 华北电力大学微型计算机,华北电力大学 微机原理与应用
  7. 腾讯、阿里、搜狐、人人、去哪儿、迅雷等互联网企业产品笔试题目(附个人答案)
  8. 在LaTeX中添加Visio绘图
  9. 回顾RHCE——邮件收发实验
  10. 金蝶KIS商贸版开发销售出库单、销售订单带商品图片打印单据