[Leetcode Series] [] [difficult] algorithm stops alone

topic:

Topic links:  https://leetcode-cn.com/problems/sudoku-solver/

 

Problem-solving ideas:

Some time ago happened to write one, directly used the =, =

Problem-solving ideas Reference:  [Miscellany] Sudoku solver

Because the format to be adapted to this question, do a little modification

 

Code:

class Solution:
    # 判断是否是有效的数独
    def is_valid(self, row, column, rect_num):
        rect_num = row // 3 * 3 + column // 3
        if self.shudu_array[row][column] == '.':
            return False
 
        curr_num = self.shudu_array[row][column]
        if curr_num in self.rec['row'][row]:
            return False
        elif curr_num in self.rec['column'][column]:
            return False
        elif curr_num in self.rec['rect'][rect_num]:
            return False
 
        return True
 
    # 主流程函数
    def get_res(self):
        row, column = self.get_zero_pos()
        # 如果找不到需要填写数字的位置,则已求得解,递归结束
        if row >= 9 or column >= 9:
            return True
 
        # 小方格的编号
        rect_num = row // 3 * 3 + column // 3
        for val in range(1, 10):
            val = str(val)
 
            self.shudu_array[row][column] = str(val)
            # 判断填入数字后,是否是有效的数独
            if not self.is_valid(row, column, rect_num):
                self.shudu_array[row][column] = '.'
                continue
 
            # 更新记录表的行、列和小方格数据
            self.rec['row'][row].append(val)
            self.rec['column'][column].append(val)
            self.rec['rect'][rect_num].append(val)
            
            # 判断是否填入成功
            if self.get_res():
                return True
 
            # 搜索失败,还原本次操作,继续下次循环
            self.shudu_array[row][column] = '.'
            self.rec['row'][row].pop()
            self.rec['column'][column].pop()
            self.rec['rect'][rect_num].pop()
 
        return False
 
    # 获取下个需要填写数字的行列下标
    def get_zero_pos(self):
        for row in range(0, 9):
            for column in range(0, 9):
                if '.' == self.shudu_array[row][column]:
                    return row, column
                    
        return row + 1, column + 1

    def solveSudoku(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """        # 要解的原始数独
        self.shudu_array = board
        # 用于记录当前每行、每列、每个小方格已填入的数组,用于判断是否是有效的数独
        self.rec = {'row' : {0 : [], 1 : [], 2 : [], 3 : [], 4 : [], 5 : [], 6 : [], 7 : [], 8 : [], 9 : []},\
              'column' : {0 : [], 1 : [], 2 : [], 3 : [], 4 : [], 5 : [], 6 : [], 7 : [], 8 : [], 9 : []},\
              'rect' : {0 : [], 1 : [], 2 : [], 3 : [], 4 : [], 5 : [], 6 : [], 7 : [], 8 : [], 9 : []}}
        
        # 根据原始数独数组,初始化记录表
        for row in range(0, 9):
            for column in range(0, 9):
                rect_num = row // 3 * 3 + column // 3
                if self.shudu_array[row][column] == '.':
                    continue
                    
                curr_num = self.shudu_array[row][column]
                self.rec['row'][row].append(curr_num)
                self.rec['column'][column].append(curr_num)
                self.rec['rect'][rect_num].append(curr_num)
                
        self.get_res()
        board = self.shudu_array

 

Published 100 original articles · won praise 4 · Views 1466

Guess you like

Origin blog.csdn.net/songyuwen0808/article/details/105328842