最难数独的快速解法 - python

python3、numpy的学习
递归算法
参考:Python秒解最难数独 - 杨仕航的博客
http://yshblog.com/blog/74

世界最难数独

image.png

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

数独解法

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

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

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

    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/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]]

源码:https://github.com/kevinqqnj/sudo-py3/

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

image.png

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,002评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,777评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,341评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,085评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,110评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,868评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,528评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,422评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,938评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,067评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,199评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,877评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,540评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,079评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,192评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,514评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,190评论 2 357

推荐阅读更多精彩内容

  • 由于上篇的算法存在一些不足,我们不免要继续研究数独游戏的完全解,以获得更高效高质量的生成算法,对于完全解的生成过程...
    Chris啊飞飞阅读 8,043评论 0 1
  • 喜欢看《最好的我们》的人不难发现,学霸余淮特别喜欢做数独,在上课的时候也会做数独题。最近重看电视剧,想起官网上的那...
    晨曦爱读书阅读 13,422评论 5 40
  • 作为师范院校外语系的一员,我听过最多的问题就是 ——“你既然不想当老师那干嘛还读这个学校啊?” 我一般都归结于高考...
    howmaooo阅读 1,066评论 2 3
  • 或许只想给你写信 却从未有过 我不是不想给你写信 只是总写不清自己的心情 浪漫的话语多么好听 赞美的文字多么唯美 ...
    Medon阅读 276评论 0 0
  • 2018年12月底,就要进入考研的考场,未来的N个未知,让我焦虑。一直以来,好像都没有拼劲全力去做一件事情 我的高...
    南极的北浪人阅读 219评论 0 0