python自动解数独教学(Python判断有效的数独算法示例)
python自动解数独教学
Python判断有效的数独算法示例本文实例讲述了Python判断有效的数独算法。分享给大家供大家参考,具体如下:
一、题目
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
1. 数字 1-9 在每一行只能出现一次。
2. 数字 1-9 在每一列只能出现一次。
3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。
数独部分空格内已填入了数字,空白格用 ‘.' 表示。
例1:
输入:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
输出: true
例2:
输入:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
二、解法
- 先创建三个空数组 row、col、cell,以 cell 为例,里面的每个空字典都代表一个 3×3单元格,然后我们需要把数据一个个填进去
- 遍历整个二维数组,然后边遍历边把数组分别存入到 行 row , 列 col , 3×3单元格 cell 内的字典,存为key ,而不是 value 。
- 然后我们就可以判断,行、列、3×3单元格 对应的字典内是否已经存在board[x][y]这个键名,如果存在,那么说明重复了,返回 False
- 注意,字典中的值这里都为1,但是没有任何意义,你可以随意更改
- 把数组存入 3×3的单元格是一个难点,num = 3*(x//3)+y//3,这个式子是关键,可以找个数独,然后代入进去好好理解下
- 当然你也可以不用这个式子,用if/else语句来判断也行,那样比较好理解,但是不如这个式子简洁
- 类似于: if y<3 : ... elif 3<=y<6 : ... elif 6<=y : ...,
代码如下:
|
#row,col,cell分别代表行,列,3x3单元格 row, col, cell = [{}, {}, {}, {}, {}, {}, {}, {}, {}], [{}, {}, {}, {}, {}, {}, {}, {}, {}], [{}, {}, {}, {}, {}, {}, {}, {}, {}] for x in range ( 9 ): for y in range ( 9 ): #取得单元格 num = 3 * (x / / 3 ) + y / / 3 temp = board[x][y] #不需要存入 '.' if temp ! = '.' : if (temp not in row[x] and temp not in col[y] and temp not in cell[num]): row[x][temp] = '1' col[y][temp] = '1' cell[num][temp] = '1' else : return False return True |
时间 64ms,击败了 99.3%
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/linfeng886/article/details/82778890
- python strip用法(Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析)
- matlab嵌入python代码(python调用matlab的m自定义函数方法)
- python面向对象的介绍(Python面向对象思想与应用入门教程类与对象)
- python函数大全详细(详解Python函数式编程—高阶函数)
- 如何用python处理excel表格(零基础使用Python读写处理Excel表格的方法)
- python从零开始学习人工智能(为何人工智能AI首选Python?读完这篇文章你就知道了推荐)
- python模块使用方法(详解python的argpare和click模块小结)
- python使用aes加密解密(python实现AES和RSA加解密的方法)
- python读取和写入数据excel(Python向excel中写入数据的方法)
- python str类型怎么转换(Python3中的bytes和str类型详解)
- python语法规则讲解(计算机二级python学习教程2 python语言基本语法元素)
- 使用python制作游戏(python制作填词游戏步骤详解)
- python画折线图(python使用Plotly绘图工具绘制水平条形图)
- python 绕过密码(一小段Python代码,破解加密zip文件的密码)
- python 并发传输文件(python单线程文件传输的实例C/S)
- python多线程并发使用场景(对python多线程SSH登录并发脚本详解)
- 被骂欺师灭祖,与郭德纲公开叫板,何云伟改名何沄伟,开始画画了(与郭德纲公开叫板)
- 相声转行影帝,被何晴抛弃,甩10年女友闪婚生子,刘威不靠谱情史(相声转行影帝被何晴抛弃)
- 岳云鹏不说相声,改行演员了 网友 快回来说相声(岳云鹏不说相声)
- 乔欣首演古装大女主,颜值演技双在线(乔欣首演古装大女主)
- 于正又推女性古装大剧 杨蓉乔欣演女配,两位女主成 重头戏(于正又推女性古装大剧)
- 乔欣古装女主戏获热度 作为女主,却没吃到红利(乔欣古装女主戏获热度)
热门推荐
- sqlserver查看某个库所有表(SQL Server怎么找出一个表包含的页信息Page)
- python常用的切片操作(使用python PIL库实现简单验证码的去噪方法步骤)
- 微信公众号怎么开发页面(微信公众平台开发教程②微信端分享功能图文详解)
- css画三角形几种方法(利用CSS绘制任意角度的扇形示例代码)
- ASP.NET cache缓存的用法
- laravel框架知识点(Laravel 不同生产环境服务器的判断实践)
- Get和Post的区别
- php创建多个文件(PHP实现单文件、多个单文件、多文件上传函数的封装示例)
- sql server2005如何配置(SQLServer2005创建定时作业任务)
- python中for循环计算1-100(python 用for循环实现1~n求和的实例)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9