Python描述 LeetCode 37. 解数独

Posted 亓官劼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python描述 LeetCode 37. 解数独相关的知识,希望对你有一定的参考价值。

Python描述 LeetCode 37. 解数独

  大家好,我是亓官劼(qí guān jié ),在【亓官劼】公众号、CSDN、GitHub、B站等平台分享一些技术博文,主要包括前端开发、python后端开发、小程序开发、数据结构与算法、docker、Linux常用运维、NLP等相关技术博文,时光荏苒,未来可期,加油~

  如果喜欢博主的文章可以关注博主的个人公众号【亓官劼】(qí guān jié),里面的文章更全更新更快。如果有需要找博主的话可以在公众号后台留言,我会尽快回复消息.


本文原创为【亓官劼】(qí guān jié ),请大家支持原创,部分平台一直在恶意盗取博主的文章!!! 全部文章请关注微信公众号【亓官劼】。

题目

编写一个程序,通过填充空格来解决数独问题。

数独的解法需 遵循如下规则

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)

数独部分空格内已填入了数字,空白格用 '.' 表示。

示例 1:

输入:board = [["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"]]
输出:[["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
解释:输入的数独如上图所示,唯一有效的解决方案如下所示:

提示:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] 是一位数字或者 '.'
  • 题目数据 保证 输入数独仅有一个解

解题思路

DFS一路向前搜索,挨个将空格填上数字,当全部满足时,返回结束循环

Python描述

class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        global complete_flag
        line = [[0 for _ in range(10)] for __ in range(9)]
        col = [[0 for _ in range(10)] for __ in range(9)]
        jiu = [[[0 for _ in range(10)] for __ in range(3)] for ___ in range(3)]
        complete_flag = False

        # 初始化
        for i in range(9):
            for j in range(9):
                tmp = int(board[i][j]) if board[i][j] != '.' else 0
                if tmp == 0:
                    continue
                line[i][tmp] += 1
                col[j][tmp] += 1
                jiu[i // 3][j // 3][tmp] += 1

        def whether_fit(i, j, num):
            # 检测当前位置是否能放num
            return line[i][num] + col[j][num] + jiu[i // 3][j // 3][num] == 0

        def dfs(i, j):
            global complete_flag
            if complete_flag:
                return
            if i == j == 8 and board[i][j] != '.':
                complete_flag = True
                return
            if board[i][j] != '.':
                n_j = (j + 1) % 9
                n_i = i if n_j != 0 else i + 1
                dfs(n_i, n_j)
                return
            for ch in "123456789":
                tmp = int(ch)
                if whether_fit(i, j, tmp):
                    board[i][j] = ch
                    line[i][tmp] += 1
                    col[j][tmp] += 1
                    jiu[i // 3][j // 3][tmp] += 1

                    n_j = (j + 1) % 9
                    n_i = i if n_j != 0 else i + 1
                    if n_i == 9:
                        complete_flag = True
                        return
                    dfs(n_i, n_j)
                    if complete_flag:
                        return
                    board[i][j] = "."
                    line[i][tmp] -= 1
                    col[j][tmp] -= 1
                    jiu[i // 3][j // 3][tmp] -= 1

        dfs(0, 0)
开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于Python描述 LeetCode 37. 解数独的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode No.37 解数独(回溯)

LeetCode 37.解数独

leetcode——37.解数独

LeetCode-37.解数独

算法leetcode|37. 解数独(rust重拳出击)

LeetCode 37. Sudoku Solver —— 解数独