Lintcode Easy title: Valid Sudoku Judge Sudoku is legal

Source: Internet
Author: User
Tags lintcode

Topic:

Judging whether Sudoku is legal

please determine if a Sudoku is valid. The Sudoku may only be populated with partial numbers, where missing numbers are used. Said. Sample Example The following is an example of a legal sudoku. Note that a valid Sudoku (partial padding only) is not necessarily solvable. We just need to make the padding space valid. Description What is Sudoku? http://sudoku.com.au/TheRules.aspxhttp://baike.baidu.com/subview/961/10842669.htm Problem Solving:it feels so hard to not know how to proceed, see here, just to judge each row, each class, each small 3*3 matrix within the number is 1-9 is legal, but here only the number of parts, according to said, there is no fill the number think that the position is legitimate, here's the program, it is the same, feel this is wrong , but it means: as long as the partial filling of Sudoku satisfies the conditions of Sudoku, it will be able to fill the number of Sudoku . < correct or not, I'm not sure, but that's the way the program is written, the inverse of the conclusion >Java Program:
classSolution {/**      * @paramboard:the Board@return: Wether the Sudoku is valid*/     Public BooleanIsvalidsudoku (Char[] board) {        Boolean[] visited =New Boolean[9]; //Row         for(inti=0;i<9;i++) {Arrays.fill (visited,false);//every element in the visited array is filled with false             for(intj=0;j<9;j++){                if(!process (Visited,board[i][j]))return false; }        }                //Col         for(inti=0;i<9;i++) {Arrays.fill (visited,false);  for(intj=0;j<9;j++)                if(!process (Visited,board[j][i]))return false; }        //Sub Matrix         for(intI=0;i<9;i+=3)             for(intJ=0;j<9;j+=3) {Arrays.fill (visited,false);  for(intK = 0;k<9;k++)                    if(!process (visited,board[i+ k/3][j + k%3]))                        return false; }        return true; }    Private BooleanProcessBoolean[] visited,Chardight) {        if(dight== '. '))            return true; intnum = dight-' 0 '; if(num<1 | | num>9 | | visited[num-1])            return false; Visited[num-1] =true; return true; }};
View Code

Total time: 822 ms

Python program:

classSolution:#@param board, a 9x9 2D array    #@return A Boolean    defIsvalidsudoku (self, Board):#Sub Matrix         forIinchRange (0,9,3):             forJinchRange (0,9,3): Visited= [false]*9 forKinchRange (3): M= i +k N= j +kif(Self.process (visited,board[m][n]) = =False):returnFalse#Row         forIinchRange (9): Visited= [false]*9 forJinchRange (9):                if(Self.process (visited,board[i][j]) = =False):returnFalse#Col         forJinchRange (9): Visited= [false]*9 forIinchRange (9):                if(Self.process (visited,board[i][j]) = =False):returnFalsereturnTruedefprocess (self,visited,digit):ifdigit=='.':            returnTrue Num=int (digit)if(num<1orNum>9orvisited[num-1]==True):returnFalse Visited[num-1] =TruereturnTrue
View Code

Total time: 148 ms

in judging the 3*3 matrix, according to my method is very simple oh

There is also the ability to determine whether rows and columns meet the conditions can also be AC

so I put the judgment matrix on the top of the 1-9 composition.

Lintcode Easy title: Valid Sudoku Judge Sudoku is legal

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.