Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.
The Sudoku board could be partially filled, where empty cells is 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.
This problem let us verify whether a phalanx is a single matrix, judging the standard is to see whether each row of columns have duplicate numbers, and each small 3x3 small square inside whether there are duplicate numbers, if there is no repetition, then the current matrix is a Sudoku matrix, but does not mean that the number of Sudoku matrix has a solution, Simply judge whether the currently unfinished matrix is a Sudoku matrix. So, according to the definition of the Sudoku matrix, when we traverse each number, we look at the row and column containing the current position and whether the number has appeared in the 3x3 small square, then we need three sign matrix, record each row, each column, whether the small square squares appear a certain number, its row and column Mark subscript good correspondence, is the small square of the subscript need to be slightly converted, the specific code is as follows:
classSolution { Public: BOOLIsvalidsudoku (vector<vector<Char> > &Board) { if(Board.empty () | | board[0].empty ())return false; intm = Board.size (), n = board[0].size (); Vector<vector<BOOL> > Rowflag (M, vector<BOOL> (n,false)); Vector<vector<BOOL> > Colflag (M, vector<BOOL> (n,false)); Vector<vector<BOOL> > Cellflag (M, vector<BOOL> (n,false)); for(inti =0; I < m; ++i) { for(intj =0; J < N; ++j) {if(Board[i][j] >='1'&& Board[i][j] <='9') { intc = Board[i][j]-'1'; if(Rowflag[i][c] | | colflag[c][j] | | cellflag[3* (I/3) + J/3][C])return false; ROWFLAG[I][C]=true; COLFLAG[C][J]=true; cellflag[3* (I/3) + J/3][C] =true; } } } return true; }};
[Leetcode] Valid Sudoku Verification Sudoku