leetcode@ [approx] Valid Sudoku

Source: Internet
Author: User

https://leetcode.com/problems/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.

classSolution { Public: Vector<int> Getidx (intIintj) {Vector<int> idx (4); intRow, col; if(i>=0&& i<=2) {idx[0] =0; idx[1] =2; }        Else if(i>=3&& i<=5) {idx[0] =3; idx[1] =5; }        Else if(i>=6&& i<=8) {idx[0] =6; idx[1] =8; }                if(j>=0&& j<=2) {idx[2] =0; idx[3] =2; }        Else if(j>=3&& j<=5) {idx[2] =3; idx[3] =5; }        Else if(j>=6&& j<=8) {idx[2] =6; idx[3] =8; }                returnidx; }    BOOLCheckrowandcolumn (vector<vector<Char>>& Board,intIintj) {if(i<0|| I>=board.size () | | j<0|| j>=board[0].size ())return false;  for(intNi=0; Ni<board.size (); + +ni) {            if(ni = = I | | board[ni][j] = ='.')Continue; if(Board[ni][j] = = Board[i][j])return false; }                 for(intnj=0; nj<board[0].size (); + +NJ) {            if(NJ = = J | | board[i][nj] = ='.')Continue; if(Board[i][nj] = = Board[i][j])return false; }                return true; }        BOOLChecklocal (vector<vector<Char>>& Board,intIintj) {if(i<0|| I>=board.size () | | j<0|| j>=board[0].size ())return false; Vector<int> idx =Getidx (i, j); intLi = idx[0], RI = idx[1], LJ = idx[2], RJ = idx[3];  for(intp=li;p<=ri;++p) { for(intq=lj;q<=rj;++q) {if((i==p && j==q) | | board[p][q] = ='.')Continue; if(Board[i][j] = = Board[p][q])return false; }        }                return true; }    BOOLIsvalidsudoku (vector<vector<Char>>&Board) {         for(intI=0; I<board.size (); + +i) { for(intj=0; J<board[i].size (); + +j) {if(Board[i][j] = ='.')Continue; if(!checklocal (board, I, j) | |!checkrowandcolumn (BOARD, I, J))return false; }        }        return true; }};
View Code

[email protected] [approx] Valid 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.