Determine if a 9x9 sudoku is valid. Just follow the rules below to verify that the numbers you have filled are valid.
1-9
the number can appear only once per line.
- Numbers
1-9
can appear only once in each column.
- The numbers
1-9
3x3
can only appear once in each palace separated by a thick solid line.
is a partially populated, valid Sudoku.
Sudoku part of the space has been filled in the number, the blank lattice with the ‘.‘
expression.
C + + (reference to the Great God's solution)
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] Array--Effective Sudoku