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.
The subject is to test whether a 9x9 matrix satisfies the characteristics of Sudoku. That is, there are numbers or '. ', to determine whether the existing figures are in accordance with the nine Gongge rules. The idea is to open up three 9*9 bool, respectively, represent row row, column col, and sub-nine Subsudo, judge whether there is illegal numbers , if there is a judgment on the output is illegal, otherwise, until the end. There is no illegal element, then output true. Time: 12ms. The code is as follows:
classSolution { Public: BOOLIsvalidsudoku (vector<vector<Char>>&Board) { if(Board.size ()! =9) return false; BOOLrow[9][9], col[9][9], subsudo[9][9]; memset (Row,false,sizeofrow); memset (col,false,sizeofcol); memset (Subsudo,false,sizeofSubsudo); for(size_t i =0; I <9; ++i) { if(Board[i].size ()! =9) return false; for(size_t j =0; J <9; ++j) { if(Board[i][j] = ='.') Continue; intc = Board[i][j]-'1'; if(Row[i][c] | | col[j][c] | | subsudo[3* (I/3) + J/3][c])return false; ROW[I][C]= Col[j][c] = subsudo[3* (I/3) + J/3][C] =true; } } return true; }};
[Leetcode] #36 Valid Sudoku