Wednesday, April 23, 2014

LeetCode: Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.


A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.


Just follow the rules, check every row, every column and every block.

C++ Code:

/*
 * func: valid_sudoku
 * goal: to check if the sudoku board is valid
 * @param board: vector of sudoku board
 * return: true or false
 */
bool valid_sudoku(vector<vector<char> > &board){
    assert(board.size() == 9);
    for(int i=0; i<9; i++){
        assert(board.at(i).size() == 9);
    }
    
    vector<int> check_helper(9, 0);
    //Check each row
    for(int row=0; row<9; row++){
        for(int column=0; column<9; column++){
            if(board[row][column] != '.'){
                if(check_helper[board[row][column] - '0'] != 0){
                    return false;
                }else{
                    check_helper[board[row][column] - '0'] = 1;
                }
            }
        }
        fill(check_helper.begin(), check_helper.end(), 0);
    }
    
    //Check each column
    for(int column=0; column<9; column++){
        for(int row=0; row<9; row++){
            if(board[row][column] != '.'){
                if(check_helper[board[row][column] - '0'] != 0){
                    return false;
                }else{
                    check_helper[board[row][column] - '0'] = 1;
                }
            }
        }
        fill(check_helper.begin(), check_helper.end(), 0);
    }
    
    //Chech each block
    for(int row = 0; row < 9; row += 3){
        for(int column = 0; column < 9; column += 3){
            for(int row_offset = 0; row_offset < 3; row_offset++){
                for(int column_offset = 0; column_offset < 3; column_offset++){
                    if(board[row + row_offset][column + column_offset] != '.'){
                        if(check_helper[board[row + row_offset][column + column_offset] - '0'] != 0){
                            return false;
                        }else{
                            check_helper[board[row + row_offset][column + column_offset] - '0'] = 1;
                        }
                    }
                }
            }
            fill(check_helper.begin(), check_helper.end(), 0);
        }
    }
    
    return true;
}

Python Code:

# func: To check if the current Sudoku board is a valid one
# @param board: sudoku board
# @return: True or False
def valid_sudoku(board):
    assert len(board) == 9
    for row in board:
        assert len(row) == 9

    #Check every row
    check_helper = set()
    for row in board:
        for element in row:
            if element != '.':
                if element in check_helper:
                    return False
                else:
                    check_helper.add(element)

        check_helper.clear()

    #Check every column
    for column in xrange(9):
        for row in xrange(9):
            if board[row][column] != '.':
                if board[row][column] in check_helper:
                    return False
                else:
                    check_helper.add(board[row][column])
        check_helper.clear()

    #Check every block
    for row in [0, 3, 6]:
        for column in [0, 3, 6]:
            for row_offset in xrange(3):
                for column_offset in xrange(3):
                    if board[row + row_offset][column + column_offset] != '.':
                        if board[row + row_offset][column + column_offset] in check_helper:
                            return False
                        else:
                            check_helper.add(board[row + row_offset][column + column_offset])
            check_helper.clear()
    return True

No comments:

Post a Comment