Python implements the code for solving the sudoku program.

Source: Internet
Author: User

Python implements the code for solving the sudoku program.

I accidentally found that a Sudoku game came along with the linux system and played a few games. Instead, I was a Sudoku cainiao. I had never played it before, and I had to make a mess in a few steps.

Therefore, it is fun to use the powerful computing power of the computer to solve the problem of data independence.

Below we will record some of my ideas and experiences in writing a Sudoku program.

I. Basic solutions for Sudoku games

In general, programming is a methodology. No matter what program, the problem solving process must be divided into several simple methods that can be implemented by computers. As the saying goes, the greatest truths are simple. For computers that can only understand 0 and 1, more steps are required to solve the problem step by step.

First, let's take a look at the basic concepts of Data independence.

A total of grids are separated into nine grids. The rule is simple-the numbers in each cell are required to ensure that they are not in the horizontal and vertical bars and the same number in the nine cells.

So our general idea is to try to fill in the number starting from the first space and starting from 1. If 1 does not meet the requirement that there are no duplicates in the horizontal and vertical bars, then fill in 2 again, and so on, wait until you enter a number that meets the rule temporarily, interrupt the grid, and move to the next space to repeat the process.

If a space is found to be available in countless ways, it indicates that the previous one is filled in incorrectly, then the previous one is returned and the last one is interrupted, until this is done, the correct format is returned.

In this way, we can organize the important steps:

• Find the next space
• Fill in numbers 1 to 9 in turn
• Recursively determine whether the number of filled items meets the rules

Ii. Procedures

The first test of Sudoku is the most difficult sudoku in the world so far, which was designed by the Finnish mathematician inkara for three months. As follows:

The space is represented by 0, and the number of independent columns is represented as a nested list. In this way, the number of rows and columns in each grid are exactly the indexes 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 def check (self, x, y, value): # check whether the same item for row_item in self exists in each column and in each row. 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 [row] [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 ): # obtain the next unfilled item for next_soulu in range (y +): if self. B [x] [next_soulu] = 0: return x, next_soulu for row_n in range (x +): for col_n in range (): if self. B [row_n] [col_n] = 0: return row_n, col_n return-1,-1 # if no other item is left blank,-1 def try_it (self, x, y): # main loop if self. B [x] [y] = 0: for I in range (): # Try self from 1 to 9. t + = 1 if self. check (x, y, I): # Meets the unconditional self. B [x] [y] = I # Fill in the 0 lattice next_x, next_y = self. get_next (x, y) # obtain the next 0 lattice. if next_x =-1: # if no 0 lattice exists, return True # return True else: # if the next 0 lattice exists, recursively determine the next 0 lattice until it is filled with Sudoku end = self. try_it (next_x, next_y) if not end: # There are non-conforming items in the recursion process, that is, the try_it function returns the None item self. B [x] [y] = 0 # go back to the previous layer and continue else: return True def start (self): begin = datetime. datetime. now () if self. B [0] [0] = 0: self. try_it (0, 0) else: 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 ([, 0, 0], [, 0, 0], [, 0], [,], [, 0], [, 8], [, 0], [, 0]) 73 s. start ()

It is worth noting that the recursive judgment can be cleverly rolled back to the previous layer when the wrong branch is taken. The specific implementation is to use the for loop to continuously fill in numbers from 1 to 9 and achieve the breakpoint in the record at the same time. The Return Value of the next layer is used to determine whether to restore the data.

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

We can see that although the program has a large number of operations, it is still very fast.

Related Article

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.