Scribbling

37. Sudoku Solver 본문

Computer Science/Coding Test

37. Sudoku Solver

focalpoint 2021. 9. 6. 21:53
class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        num_left = 0
        for i in range(9):
            for j in range(9):
                if board[i][j] == '.':
                    num_left += 1
        self.solver(board, num_left)     
        
        
    def solver(self, board, num_left):
        if num_left == 0:
            return True
        
        # Search Blank -> (i, j)
        start_y, start_x = 0, 0
        found_flag = False
        for t in range(1, 10):
            for i in range(start_y, start_y+3):
                for j in range(start_x, start_x+3):
                    if board[i][j] == '.':
                        found_flag = True
                        break
                if found_flag:
                    break
            if found_flag:
                break
            start_x += 3
            if t % 3 == 0:
                start_y += 3
                start_x = 0
                
        candidates = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
        excludes = set()
        
        # Square
        start_y, start_x = i // 3 * 3, j // 3 * 3
        for y in range(start_y, start_y+3):
            for x in range(start_x, start_x+3):
                if board[y][x] != '.':
                    excludes.add(board[y][x])
                
        # Horizontal
        for x in range(9):
            if board[i][x] != '.':
                excludes.add(board[i][x])
        
        # Vertical
        for y in range(9):
            if board[y][j] != '.':
                excludes.add(board[y][j])
        
        candidates = [candidate for candidate in candidates \
                      if candidate not in list(excludes)]
        
        for num in candidates:
            board[i][j] = num
            if self.solver(board, num_left - 1):
                return True
        board[i][j] = '.'
        return False

'Computer Science > Coding Test' 카테고리의 다른 글

LeetCode: 46. Permutations  (0) 2021.09.07
LeetCode: 40. Combination Sum II  (0) 2021.09.07
36. Valid Sudoku  (0) 2021.09.06
44. Wildcard Matching  (0) 2021.09.06
LeetCode: 48. Rotate Image  (0) 2021.09.05