Data independence optimization algorithms (1)

Source: Internet
Author: User

A few days ago, I suddenly remembered that when I participated in the American mathematical modeling competition, I did not come up with the topic about Sudoku. So now I have studied it again and I feel that my thinking is no longer as messy as before, it's hard to think about it! Inspired by the author of http://www.cnblogs.com/carysun/archive/2009/10/07/jiugong.html, his article was written to solve an existing Sudoku. My current question was to generate a new Sudoku. When I saw his algorithm, I found that this problem was not as profound as I previously thought. Thank you for your article!

  • What is DT?

First, let's talk about Data independence ~~~ In general, DT alone is a 9*9-arranged square table with a total of 81 squares. Of course, it can also have different sizes, but it is usually 9*9. You can enter 1 ~ in each square ~ 9. It also has requirements!

1. Numbers in the same row cannot be repeated!

2. Numbers in the same column cannot be repeated!

3. There are also nine numbers in the 3*3 Nine cells that cannot be repeated!

This may sound hard to understand ,!

 

It is easy to understand that there are no repeated numbers in the row and column. The third requirement is to divide the 81 cells of 9*9 into nine 3*3 9 cells with clear demarcation lines. The numbers in these 9 3*3 cells can only be 1 ~ 9.

This is just to generate a Sudoku, and then hide some of the grids and you will not be able to see the numbers, so that you can enter them according to the rules. This is now a common Sudoku game! The more grids you need to fill in, the higher the difficulty. At the same time, there may be multiple solutions! For example:

  • How to solve Sudoku?

Next I will first explain the solution to the sashboard. The specific code can be found at the url I gave above. To put it simply, it is to traverse each lattice to be filled in, a number and a number. If you find that a grid cannot meet the requirements, roll back to the previous grid and try another number to get the correct answer. For example:

1. Enter 1 for the first check box. If the verification is correct, enter the second check box.

2. If the second check box is set to 2, enter the third check box.

3. If the third grid is filled with 1 ~ 9 if any number does not meet the requirements, the second lattice is returned.

4. You cannot enter 2 in the second lattice, and you need to try the next number again.

5. Similarly, if all the numbers in the second grid have been tried and cannot meet the requirements, the system returns to the first grid and tries the next number ..

6. and so on

Try a correct solution!

If you calculate the number, it is estimated that it will not be so stupid. For example, I think you will first check which grid of candidates is unique.

The concept of the number of candidates is mentioned here. In theory, each grid has 1 ~ There are nine possible numbers to fill in, but this is not the case for a sudoku with some grids already filled in. We all know that if a number already appears in the same row, column, or cell, the number cannot be entered in other cells. That is to say, it seems that there are nine numbers for each grid. Actually there are not so many! The figure looks intuitive:

The red number in the square in the figure indicates the number that can be filled in the square currently. Obviously, the picture on the left is incorrect. There are not so many numbers in the square, the numbers 1, 3, 4, 5, 7, 8, and 9 do not meet the requirements. Only 2, 6 can be entered. The figure on the right is the valid number in the current box. We call these numbers the candidate number for this box!

 

Next, what is your first step to fill in the statement? The fewer candidates ~~~ If the number of candidates is only one, it means that this cell can only fill in the unique number of candidates, so that we have fewer spaces, and each time we fill in a number, the number of related cells is also reduced. The "correlation" mentioned here refers to other cells in the same column as the cell and in the same cell. For example, in the figure above, if we enter 2 in the cell marked with the number of candidates, in this case, the number of candidates for all cells in the same cell column as the number of cells in the same column as the number of cells in the nine cells should be excluded.

But the computer is not so intelligent. The return method not only traverses a grid in order, but also tries every number. That is to say, every attempt is filled with a number, you need to determine whether this number is appropriate in rows, columns, and nine cells.

  • How to generate Sudoku

You can also generate a Sudoku based on the principle of solving the sudoku. Traversing from the first lattice is actually just like solving all the blank Sudoku. However, the space complexity and time complexity of this algorithm are not ideal. One of the main reasons is what we mentioned above, each time you try to enter a number in a grid, you must determine whether the number in the related grid already exists. To be accurate, there are a total of related cells: rows + columns + nine squares (excluding yourself) = 8 + 8 + 4 = 20. For example:

 

Assume that the blue lattice in the middle is the one we want to fill in. If we use the previous method to solve the sudoku, we need to determine whether the number exists in all the 20 red grids. Each grid has 1 ~ 9 A total of 9 numbers are selected. That is to say, in the case of the least coincidence, we need to make 9*20 = 180 judgments every time we fill in a grid, and if it is not so clever, 1 ~ 9 These 9 numbers cannot meet the requirements, so we need to return to the previous cell and re-select the number, which all needs to be computed again with a cup of cake !...

Suppose we only fill in the last grid, as shown in the above analysis, we have to make 180 judgments at the worst. If we have the last two grids, the worst case is to make 1802 judgments... From the first entry to the last generation of the total number of Sudoku, the worst case of the computer needs to be judged 18081 times .... If our computer knows that he wants to count so many times in advance, I don't think it will do anything...

Is there a better way to implement it?

We have discussed the number of candidates before. Imagine that if you are a candidate, you will definitely choose one of the candidates to fill in the cells, because the number of non-candidates certainly does not meet the conditions. How Can Computers know the number of candidates? There is another problem. If we find that a cell has only one candidate number, we must first fill in the number for that cell (because its value must be the only candidate number ), then fill in other cells to reduce the number of candidates for many cells. However, the same problem occurs again. Our computer uses the above algorithms to fill in the number of cells and cells in order. It does not jump to the cell with the unique number of candidates to fill in the number, what's more, it doesn't know which cell has only one candidate!

Now that you know the problem, it's time to solve it ~~~

We can define a cell class so that it has the attribute of candidate number. Every time we try to fill in the number, we can update the candidate number attribute of cells related to it!

Next, we will begin to compile this program to optimize and recursively generate the sudoku. Next release tomorrow!

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.