算法题--求解数独

image.png

0. 链接

题目链接

解题参考链接

1. 题目

Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
Empty cells are indicated by the character '.'.

image.png

A sudoku puzzle...

image.png

...and its solution numbers marked in red.

Note:

  • The given board contain only digits 1-9 and the character '.'.
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • The given board size is always 9x9.

2. 思路: 深度优先搜索

  • 先统计每行,每列,每个九宫格的数字集合
  • 按行列的顺序,对每个格子逐个尝试未出现过的数字,对每个假设的数字,继续在此基础上尝试后续所有格子;
  • 当一个假设不满足条件的,继续尝试下一个数字,最终找到唯一符合条件的数字。

3. 代码

class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        row_check = [set() for i in range(9)]
        col_check = [set() for i in range(9)]
        grid_check = [[set() for i in range(3)] for j in range(3)]
        
        def dfs(i, j, board, row_check, col_check, grid_check):
            if i == 9:
                return True
            
            if j == 9:
                return dfs(i + 1, 0, board, row_check, col_check, grid_check)
            
            if board[i][j] != '.':
                return dfs(i, j + 1, board, row_check, col_check, grid_check)
            
            for value in range(1, 10):
                if (value in row_check[i]) or (value in col_check[j]) or (value in grid_check[i // 3][j // 3]):
                    continue
                
                board[i][j] = str(value)
                row_check[i].add(value)
                col_check[j].add(value)
                grid_check[i // 3][j // 3].add(value)
                
                if not dfs(i, j + 1, board, row_check, col_check, grid_check):
                    board[i][j] = '.'
                    row_check[i].remove(value)
                    col_check[j].remove(value)
                    grid_check[i // 3][j // 3].remove(value)
                else:
                    return True
                
            return False
        
        for i in range(9):
            for j in range(9):
                if board[i][j] != '.':
                    row_check[i].add(int(board[i][j]))
                    col_check[j].add(int(board[i][j]))
                    grid_check[i // 3][j // 3].add(int(board[i][j]))
                    
        dfs(0, 0, board, row_check, col_check, grid_check)

4. 结果

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 7,939评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,143评论 0 13
  • 原题 Write a program to solve a Sudoku puzzle by filling th...
    rome753阅读 3,607评论 0 1
  • 那年他十五,她十三。 那时他们在一个学校念书,他学习好,球也打得好,穿着白色衬衫,高高的个子,干净润朗,光芒四射。...
    诗马集阅读 3,785评论 13 4
  • 本学期,在米校长的带领下 ,我校初中部开展了轰轰烈烈的规范仪容仪表,搜查违禁品的活动,先是各个班级自查自报,再由年...
    岁月静好_刘社美阅读 3,863评论 0 0