records/07-30/test_and.py
def solveSudoku(self, board) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        row = [set(range(1, 10)) for _ in range(9)]
        col = [set(range(1, 10)) for _ in range(9)]
        box = [set(range(1, 10)) for _ in range(9)]
        empty = []

        for i in range(9):
            for j in range(9):
                if board[i][j] != '.':
                    box_i = (i // 3) * 3 + j // 3
                    num = int(board[i][j])
                    row[i].remove(num)
                    col[j].remove(num)
                    box[box_i].remove(num)
                else:
                    empty.append((i, j))

        def sort_empty(ipt):
            i, j = ipt
            box_i = (i // 3) * 3 + j // 3
            return len(row[i] & col[j] & box[box_i])

        def backtrack():
            # search fewest candidates first
            empty.sort(key=sort_empty)  # sort the list IN PLACE
            if len(empty) == 0:
                # terminator
                return True

            i, j = empty.pop(0)
            box_i = (i // 3) * 3 + j // 3
            # for ll in cands:
            for val in row[i] & col[j] & box[box_i]:
                row[i].remove(val)
                col[j].remove(val)
                box[box_i].remove(val)
                board[i][j] = str(val)
                if backtrack():
                    return True
                row[i].add(val)
                col[j].add(val)
                box[box_i].add(val)
            empty.append((i, j))
            return False

        backtrack()

        Do not return anything, modify board in-place instead.
        
Do not return anything, modify board in-place instead.
Do not return anything modify board in - place instead .
def solveSudoku(self, board) -> None:
        row = [set(range(1, 10)) for _ in range(9)]
        col = [set(range(1, 10)) for _ in range(9)]
        box = [set(range(1, 10)) for _ in range(9)]
        empty = []
        for i in range(9):
            for j in range(9):
                if board[i][j] != '.':
                    box_i = (i // 3) * 3 + j // 3
                    num = int(board[i][j])
                    row[i].remove(num)
                    col[j].remove(num)
                    box[box_i].remove(num)
                else:
                    empty.append((i, j))
        def sort_empty(ipt):
            i, j = ipt
            box_i = (i // 3) * 3 + j // 3
            return len(row[i] & col[j] & box[box_i])
        def backtrack():
            empty.sort(key=sort_empty)  
            if len(empty) == 0:
                return True
            i, j = empty.pop(0)
            box_i = (i // 3) * 3 + j // 3
            for val in row[i] & col[j] & box[box_i]:
                row[i].remove(val)
                col[j].remove(val)
                box[box_i].remove(val)
                board[i][j] = str(val)
                if backtrack():
                    return True
                row[i].add(val)
                col[j].add(val)
                box[box_i].add(val)
            empty.append((i, j))
            return False
        backtrack()
def solveSudoku ( self , board ) -> None : row = [ set ( range ( 1 , 10 ) ) for _ in range ( 9 ) ] col = [ set ( range ( 1 , 10 ) ) for _ in range ( 9 ) ] box = [ set ( range ( 1 , 10 ) ) for _ in range ( 9 ) ] empty = [ ] for i in range ( 9 ) : for j in range ( 9 ) : if board [ i ] [ j ] != '.' : box_i = ( i // 3 ) * 3 + j // 3 num = int ( board [ i ] [ j ] ) row [ i ] . remove ( num ) col [ j ] . remove ( num ) box [ box_i ] . remove ( num ) else : empty . append ( ( i , j ) ) def sort_empty ( ipt ) : i , j = ipt box_i = ( i // 3 ) * 3 + j // 3 return len ( row [ i ] & col [ j ] & box [ box_i ] ) def backtrack ( ) : empty . sort ( key = sort_empty ) if len ( empty ) == 0 : return True i , j = empty . pop ( 0 ) box_i = ( i // 3 ) * 3 + j // 3 for val in row [ i ] & col [ j ] & box [ box_i ] : row [ i ] . remove ( val ) col [ j ] . remove ( val ) box [ box_i ] . remove ( val ) board [ i ] [ j ] = str ( val ) if backtrack ( ) : return True row [ i ] . add ( val ) col [ j ] . add ( val ) box [ box_i ] . add ( val ) empty . append ( ( i , j ) ) return False backtrack ( )
Do not return anything, modify board in-place instead.
Do not return anything modify board in - place instead .
"""
        Do not return anything, modify board in-place instead.
        """ # search fewest candidates first # sort the list IN PLACE # terminator # for ll in cands:
self
board