Python code example for implementing the coefficient-alone program

Source: Internet
Author: User
Recently in taking children to learn sudoku, occupation by the Internet search the solution of the relevant procedures, here to share to everyone, hope to learn python to help you

Accidentally found a Linux system with a Sudoku game, open to play a few. Helpless is the number of rookie, has not played before, at all, can not go out a few steps on a group of paste.

So I intend to use the powerful computing power of the computer to violent coefficient alone, or very fun.

The following is a record of my writing coefficient program of some ideas and experience.

I. Basic solutions to Sudoku Games

Programming in general, is a methodology. Regardless of the program, the problem resolution process must be broken down into a number of simple methods that can be implemented by the computer. As the saying goes, the Boulevard to Jane. For computers that can only understand 0 and 1, it is more necessary to subdivide the steps and solve the problem step-by-step.

First of all, consider the basic concept of coefficient alone.

Sudoku Horizontal Nine Vertical Nine a total of 81 lattice, at the same time divided into 9 nine Gongge. The rules are simple--you need numbers in each lattice to ensure that there is no same number in the horizontal and vertical rows and nine.

So our approximate idea is, starting from the first space to try to fill the number, starting from 1, if the 1 does not meet the horizontal vertical row nine no duplicates, then fill in 2, and so on, until the fill in a temporary meet the rules of the number, break the lattice, move to the next space to repeat the process.

If you arrive at a space to find that there are countless options, stating that one of the preceding boxes is wrong, return to the previous box, and continue to 9 from the previous break, until you have returned to the wrong grid.

In this way, we can sort out the important steps:

• Find the next space
• Fill in the grid numbers 1 to 9 in turn
• Recursion to determine if the number of fills is in accordance with the rules

Two. Procedures

The first Test Sudoku used was the most difficult sudoku in the world ever devised by a Finnish mathematician for 3 months. As follows

The spaces are represented by 0, and the Sudoku is represented as a nested list, so that the number of rows and columns per cell is exactly the index of each corresponding number in the list.

The procedure is as follows:

 #coding =utf-8 Import DateTime class Solution (Object): Def __init__ (self,board): self.b = board self.t = 0 de     F Check (Self,x,y,value): #检查每行每列及每宫是否有相同项 for Row_item in self.b[x]: if Row_item = = Value:return False For Row_all in self.b:if row_all[y] = = Value:return False row,col=x/3*3,y/3*3 row3col3=self.b[r  OW][COL:COL+3]+SELF.B[ROW+1][COL:COL+3]+SELF.B[ROW+2][COL:COL+3] for Row3col3_item in Row3col3:if Row3col3_item       = = Value:return False return True def get_next (self,x,y): #得到下一个未填项 for Next_soulu in range (y+1,9): If self.b[x][next_soulu] = = 0:return X,next_soulu for row_n in range (x+1,9): For Col_n in range (0,9 ): if self.b[row_n][col_n] = = 0:return Row_n,col_n return-1,-1 #若无下一个未填项, return-1 def try_it (self,x , y): #主循环 if self.b[x][y] = = 0:for I in range (1,10): #从1到9尝试 self.t+=1 if Self.check (x,y,i): #符合 Line The Lie are unconditionally self.B[x][y]=i #将符合条件的填入0格 next_x,next_y=self.get_next (x, y) #得到下一个0格 if next_x = =-1: #如果无下一个0格  Return True #返回True Else: #如果有下一个0格, recursively determine the next 0 block until full sudoku End=self.try_it (next_x,next_y) if Not end: #在递归过程中存在不符合条件的, even if the Try_it function returns none of the entries self.b[x][y] = 0 #回朔到上一层继续 else:r Eturn True def start (self): begin = Datetime.datetime.now () if self.b[0][0] = = 0:self.try_it (0,0) el Se:x,y=self.get_next (0,0) self.try_it (x, y) for i in self.b:print i end = Datetime.datetime.now () print ' \ncost time: ', end-begin print ' times: ', self.t return s=solution ([[8,0,0,0,0,0,0,0,0], [0, 0, 3,6,0,0,0,0,0], [0,7,0,0,9,0,2,0,0], [0,5,0,0,0,7,0,0,0], [0,0,0,8,4,5,7,0,0], [0,0,0,1,0,0,0,3,0], [0 , 0,1,0,0,0,0,6,8], [0,0,8,5,0,0,0,1,0], [0,9,0,0,0,0,4,0,0]]) S.start ()

It is important to note that the recursive judgment used can be cleverly returned to the previous layer when the wrong branch is taken. The implementation is the use of the For loop to continuously fill in numbers from 1 to 9 to achieve the role of record breakpoints. Use the return value of the next layer to determine whether to return.

The program output is as follows:

[8, 1, 2, 7, 5, 3, 6, 4, 9] [9, 4, 3, 6, 8, 2, 1, 7, 5] [6, 7, 5, 4, 9, 1, 2, 8, 3] [1, 5, 4, 2, 3, 7, 8, 9, 6] [3, 6, 9, 8, 4, 5, 7, 2, 1] [2, 8, 7, 1, 6, 9, 5, 3, 4] [5, 2, 1, 9, 7, 4, 3, 6, 8] [4, 3, 8, 5, 2, 6, 9, 1, 7] [7, 9, 6, 3, 1, 8, 4, 5, 2]cost time:0:00:00.060687times:45360

You can see that the program, although the number of operations more, but the speed is very fast.

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.