Valid Sudoku
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.
Check each row, each column, each of the nine Gongge to see if duplicate elements appear, and return True if False occurs.
The difficulty lies in representing the coordinates of each lattice point in the nine of the first I.
Observe the law of line numbers:
No. 0 Nine Gongge: 000111222; 1th Nine Gongge: 000111222, 2nd nine Gongge: 000111222;
3rd nine Gongge: 333444555; 4th nine Gongge: 333444555; 5th nine Gongge: 333444555;
6th nine Gongge: 666777888; 7th nine Gongge: 666777888; 8th nine Gongge: 666777888;
Visible for each of the three nine Gonge increase 3, for a single nine Gongge, every three lattice line number 1.
Therefore, the row number of the J-point of the ninth Gongge can be expressed as a I/3*3+J/3
Observe the rule of the column number:
No. 0 Nine Gongge: 012012012; 1th Nine Gongge: 345345345; 2nd nine Gongge: 678678678;
3rd nine Gongge: 012012012; 4th nine Gongge: 345345345; 5th nine Gongge: 678678678;
6th nine Gongge: 012012012; 7th nine Gongge: 345345345; 8th nine Gongge: 678678678;
Visible for the next nine Gongrie increase 3, the cycle period is 3, for a single nine Gongge, each lattice point line number 1, the period is 3.
The mathematical representation of a cycle is the modulo operation MoD.
Therefore, the column number of the J-point of the ninth Gongge can be expressed as a I/3*3+J/3
classSolution { Public: BOOLIsvalidsudoku (vector<vector<Char> > &Board) { for(inti =0; I <9; i + +) {Map<Char,BOOL> m1;//Check i_th rowmap<Char,BOOL> m2;//Check i_th Colmap<Char,BOOL> m3;//Check i_th sub-boxes for(intj =0; J <9; J + +) { //i_th Row, j_th Col if(Board[i][j]! ='.') { if(M1.find (board[i][j])! =m1.end ())return false; ElseM1[board[i][j]]=true; } //i_th col, j_th row if(Board[j][i]! ='.') { if(M2.find (board[j][i])! =m2.end ())return false; ElseM2[board[j][i]]=true; } //i_th sub-boxes, j_th grid if(board[i/3*3+j/3][i%3*3+j%3] !='.') { if(M3.find (board[i/3*3+j/3][i%3*3+j%3]) !=m3.end ())return false; ElseM3[board[i/3*3+j/3][i%3*3+j%3]] =true; } } } return true; }};
i%3*3+j%3
"Leetcode" Valid Sudoku