[Leetcode] Valid Sudoku Verification Sudoku

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.