[Leetcode] Sudoku Solver @ Python

Source: Internet
Author: User
Tags ord

Original title address: https://oj.leetcode.com/problems/sudoku-solver/

Test instructions

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells is indicated by the character ‘.‘ .

Assume that there would be is only one unique solution.

A Sudoku Puzzle ...

... and its solution numbers marked in red.

Problem Solving Ideas 1: (This version was tested in 2014/09/12 just passed)

Find the solution of Sudoku, dancing links will not, can only write single compression version.

http://c4fun.cn/blog/2014/03/20/leetcode-solution-02/#Sudoku_Solver

classSolution:#@param board, a 9x9 2D array    #Solve the Sudoku by modifying the input board In-place.    #Don't return any value.    defSolvesudoku (self, Board): LT, RT, BT= [0] * 9, [0] * 9, [0] * 9Self.dt= {}         forIinchRange (9): self.dt[1<<i] = Chr (ORD ('1')+i) forIinchRange (9): Board[i]=list (Board[i]) forJinchRange (9):                if(Board[i][j] = ='.'):                    Continue; Num= Ord (Board[i][j])-Ord ('1') Lt[i]|= 1 <<Num RT[J]|= 1 <<Num bt[j3*3+I//3] |= 1 <<Num SELF.DFS (board, 0, LT, RT, BT) Board= ["'. Join (s) forSinchboard]defDfs (self, board, p, LT, RT, BT): whileP < 81 andBOARD[P/9][P%9]! ='.': P+ = 1ifp = = 81:            returnTrue I, J, K= P//9, p%9, P%9//3*3+P//9//3ifBOARD[I][J]! ='.': Self.dfs (board, p+ 1, LT, RT, BT)returnTrue can= (~ (Lt[i]|rt[j]|bt[k])) & (0x1ff) Pre=Board[i] whileCan:num= can&-can board[i][j]=Self.dt[num] Lt[i]|=Num RT[J]|=Num Bt[k]|=NumifSelf.dfs (board, p + 1, LT, RT, BT):returnTrue Board[i][j]='.'Lt[i]&= ~Num RT[J]&= ~Num Bt[k]&= ~Num can-=NumreturnFalse

Problem Solving idea 2: (this version was tested in 2014/09/12, timed out, failed)

Use DFS to resolve the issue.

Http://www.cnblogs.com/zuoyuan/p/3770271.html

classSolution:#@param board, a 9x9 2D array    #Solve the Sudoku by modifying the input board In-place.    #Don't return any value.    defSolvesudoku (self, Board):defisValid (x, y): tmp=board[x][y]; board[x][y]='D'             forIinchRange (9):                ifBoard[i][y]==tmp:returnFalse forIinchRange (9):                ifBoard[x][i]==tmp:returnFalse forIinchRange (3):                 forJinchRange (3):                    ifboard[(X/3) *3+i][(Y/3) *3+j]==tmp:returnFalse Board[x][y]=tmpreturnTruedefDFS (board): forIinchRange (9):                 forJinchRange (9):                    ifboard[i][j]=='.':                         forKinch '123456789': Board[i][j]=kifIsValid (I,J) andDFS (board):returnTrue Board[i][j]='.'                        returnFalsereturnTrue DFS (board)

[Leetcode] Sudoku Solver @ Python

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.