[LeetCode 36] Valid Sudoku Solution

来源:互联网 发布:淘宝有哪几种营销方式 编辑:程序博客网 时间:2024/06/10 00:02

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.

Ideas: first of all, we need to know what is sudoku board, we could check the blog: Sudoku Puzzles - 九宫格(数独)游戏

Then, whether the soduku board is valid or not based on the following rules.

1) The width and length of the board should 9;

2) In the one row, there is only distinct number (between 1 to 9) or '.'; 

3) In one column, there is only distinct number (between 1 to 9) or '.'; 

4) In small 3 * 3 board, it should only contain the distinct number(between 1 to 9) or '.'; 

The tricks are as followings:(1) how to use vector, (2) use a vector to flag whether the digit are distinct. (3) how to get the small 3*3 subbox.

class Solution {public:    bool isValidSudoku(vector<vector<char> > &board) {        if(board.size() != 9) return false;                //row is valid, column is valid, small board is valid ?                //row is valid        for(int r = 0; r < 9; r++){            vector<bool> flag (9, false);            for(int c = 0; c < 9; c++){                if(board[r][c] == '.') continue;                                if(isDigit(board[r][c])){                    if(flag[(board[r][c] - '0')])                        return false;                    flag[(board[r][c] - '0')] = true;                }            }        }                // column is valid?        for(int c = 0; c < 9; c++){            vector<bool> flag (9, false);            for(int r = 0; r < 9; r++){                if(board[r][c] == '.') continue;                                if(isDigit(board[r][c])){                    if(flag[(board[r][c] - '0')])                        return false;                    flag[(board[r][c] - '0')] = true;                }            }        }                //small 3 * 3 is valid?        for(int r = 0; r < 9; r += 3){            for(int c = 0; c < 9; c += 3){                //how to set in a 3 * 3 sub-board                vector<bool> flag (9, false);                for(int i = 0; i < 3; i++){                    for(int j = 0; j < 3; j++){                        if(board[r + i][c + j] == '.') continue;                                                if(isDigit(board[r + i][c + j])){                            if(flag[(board[r + i][c + j] - '0')])                                return false;                            flag[(board[r + i][c + j] - '0')] = true;                        }                    }                }            }        }                return true;            }private:    bool isDigit(char i){        if (i - '0' < 10 && i - '0' >0)             return true ;        else             return false;    }};


0 0