Valid Sudoku of LeetCode

Source: Internet
Author: User

[Question]

Determine if a Sudoku is valid, according to: Sudoku Puzzles-The Rules.

The Sudoku board cocould be partially filled, where empty cells are filled with the character'.'.


A partially filled sudoku which is valid. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vcd4kpg1_pqg + zOLS4qG/PC9oND4KPHA + vNnJ6NK7uPbK/release/y3 + release/release + CjwvcD4KPHA + release/release + pgltzybzcm9 "http://www.bkjia.com/uploadfile/Collfiles/20140120/20140120134659466.jpg" alt = "\"

(2) Each column must be within the range of 1-9 and appear only once.

(3) numbers 1-9 appear only once in each uterine cell.

The sudoku cells can be partially filled, and their hollow cells are full of characters ".'.

For example:

Effective Sudoku for partial filling

[Analysis]

Detail analysis questions

(1) Check rows

(2) Check Columns

(3) Check 9 uterine Cells

[Code]

/********************************** Date: 2014-01-20 * Author: SJF0115 * question: Valid Sudoku * Source: http://oj.leetcode.com/problems/valid-sudoku/* result: AC * Source: LeetCode * Summary: * *********************************/# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include using namespace std; class Solution {public: bool isValidSudoku (vector
     
      
> & Board) {int I, j, k; // judge whether 1-9 has been used bool used [9]; for (I = 0; I <9; I ++) {// check the row memset (used, false, 9); for (j = 0; j <9; j ++) {// does not comply with the rule if (! Check (board [I] [j], used) {return false;} // if} // for // check the column memset (used, false, 9 ); for (j = 0; j <9; j ++) {// if (! Check (board [j] [I], used) {return false ;} // if} // for // check 9 uterine cells for (k = 0; k <3; k ++) {memset (used, false, 9); for (I = k * 3; I <3 * k + 3; I ++) {for (j = 0; j <3; j ++) {// non-compliant Rule if (! Check (board [I] [j], used) {return false;} // if} // for memset (used, false, 9 ); for (I = k * 3; I <3 * k + 3; I ++) {for (j = 3; j <6; j ++) {// non-compliant Rule if (! Check (board [I] [j], used) {return false;} // if} // for memset (used, false, 9 ); for (I = k * 3; I <3 * k + 3; I ++) {for (j = 6; j <9; j ++) {// non-compliant Rule if (! Check (board [I] [j], used) {return false;} // if} // for return true;} private: bool check (char c, bool used [9]) {if (c = '. ') {return true;} // character c used if (used [c-'1']) {return false ;} // character c unused else {used [c-'1'] = true; return true ;}}; int main () {Solution; bool result; vector
      
        > Board; char value [9] = {'5', '3 ','. ','. ', '7 ','. ','. ','. ','. '}; vector
       
         SubBoard (value, value + 9); board. push_back (subBoard); value = {'6 ','. ','. ', '1', '9', '5 ','. ','. ','. '}; vector
        
          SubBoard1 (value, value + 9); board. push_back (subBoard1); value = {'. ', '9', '8 ','. ','. ','. ','. ', '6 ','. '}; vector
         
           SubBoard2 (value, value + 9); board. push_back (subBoard2); value = {'8 ','. ','. ','. ', '6 ','. ','. ','. ', '3'}; vector
          
            SubBoard3 (value, value + 9); board. push_back (subBoard3); value = {'4 ','. ','. ', '8 ','. ', '3 ','. ','. ', '1'}; vector
           
             SubBoard4 (value, value + 9); board. push_back (subBoard4); value = {'7 ','. ','. ','. ', '2 ','. ','. ','. ', '6'}; vector
            
              SubBoard5 (value, value + 9); board. push_back (subBoard5); value = {'. ', '6 ','. ','. ','. ','. ', '2', '8 ','. '}; vector
             
               SubBoard6 (value, value + 9); board. push_back (subBoard6); value = {'. ','. ','. ', '4', '1', '9 ','. ','. ', '5'}; vector
              
                SubBoard7 (value, value + 9); board. push_back (subBoard7); value = {'. ','. ','. ','. ', '8 ','. ','. ', '7', '9'}; vector
               
                 SubBoard8 (value, value + 9); board. push_back (subBoard8); result = solution. isValidSudoku (board); cout <
                
                 

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.