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.
Problem Solving Ideas:
Test instructions to verify the validity of Sudoku. What is said here is the validity of the value riddle, not including whether it can be solved. The rule of Sudoku is that each row 1-9 appears once, each column 1-9 appears once, each small nine Gongge 1-9 appears only once. You can verify it in turn. A trap is that the character minus is not ' 0 ', but ' 1 '
Class Solution {Public:bool Isvalidsudoku (vector<vector<char>>& board) {//Verify that each row is valid for (int i=0; i<9; i++) {if (!checkrowvalid (board, I)) {return false; }}//Verify that each column is valid for (int i=0; i<9; i++) {if (!checkcolumnvalid (board, i)) { return false; }}//Verify that each of the cells is valid for (int i=0, i<9; i=i+3) {for (int j=0; j<9; j=j+3) {i F (!checkgridvalid (board, I, J)) {return false; }}} return true; }//Verify that each lattice is valid, passing in the upper-left corner of the subscript bool Checkgridvalid (vector<vector<char>>& board, int m, int n) { BOOL Flag[9]; memset (flag, 0, sizeof (BOOL) *9); for (int i=m, i<m+3; i++) {for (int j=n; j<n+3; J + +) {if (board[i][j]== '. ') {continue; } if (flag[board[i][j]-' 1 ']){return false; } flag[board[i][j]-' 1 ']=true; }} return true; }//Verify that each row is valid, passing in line number bool Checkrowvalid (vector<vector<char>>& board, int m) {bool Flag[9] ; memset (flag, 0, sizeof (BOOL) *9); for (int i=0; i<9; i++) {if (board[m][i]== '. ') {continue; } if (flag[board[m][i]-' 1 ']) {return false; } flag[board[m][i]-' 1 ']=true; } return true; }//Verify that each column is valid, passed in column number bool Checkcolumnvalid (vector<vector<char>>& board, int n) {bool Flag [9]; memset (flag, 0, sizeof (BOOL) *9); for (int i=0; i<9; i++) {if (board[i][n]== '. ') {continue; } if (flag[board[i][n]-' 1 ']) {return false; } flag[board[i][n]-' 1 ']=true; } return true; }};
[Leetcode] Valid Sudoku