Python判断有效的数独算法示例

作者:linfeng886 时间:2021-10-09 06:37:37 

本文实例讲述了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,数独
0
投稿

猜你喜欢

  • 高级MySQL数据库面试问题 附答案

    2024-01-13 17:38:10
  • asp如何定时执行约定的页面?

    2009-11-15 20:17:00
  • 解决使用pip安装报错:Microsoft Visual C++ 14.0 is required.

    2022-05-02 14:42:15
  • JavaScript之IE的fireEvent方法详细解析

    2024-06-05 09:11:28
  • Js sort排序使用方法

    2023-10-19 10:20:55
  • Vue如何配置根目录@(引用路径)

    2024-04-28 09:28:32
  • python爬虫使用cookie登录详解

    2023-10-13 06:36:39
  • Python在不同目录下导入模块的实现方法

    2022-03-12 09:34:52
  • Python数据类型详解(一)字符串

    2023-08-12 22:55:56
  • Python字典,函数,全局变量代码解析

    2021-02-20 06:58:58
  • PyTorch 1.0 正式版已经发布了

    2021-12-09 23:54:57
  • python实现处理mysql结果输出方式

    2024-01-28 03:30:42
  • Mysql错误1366 - Incorrect integer value解决方法

    2024-01-13 03:21:35
  • python利用opencv实现颜色检测

    2022-05-08 14:20:58
  • linux系统使用python获取内存使用信息脚本分享

    2022-10-14 07:50:53
  • python简单区块链模拟详解

    2023-11-09 12:04:57
  • 解决MySQL 5.7中定位DDL被阻塞的问题

    2024-01-14 10:20:01
  • Windows下Anaconda的安装和简单使用方法

    2022-04-21 16:46:50
  • 定义列表: DL DT DD

    2009-05-06 13:08:00
  • python中readline判断文件读取结束的方法

    2022-12-14 06:22:51
  • asp之家 网络编程 m.aspxhome.com