Sudoku is an implementation method

This article first appeared in my brief account book, recently migrated to csdn blog

Recent want to learn algorithms, inadvertently from the book to see some small game analysis and algorithm, it is actually very interesting.

Today we talk about Sudoku.

First, we introduce simple rules of Sudoku.
Sudoku
Standard Number of 9 is a single matrix of nine digits 1-9, wherein the digital matrix must meet the requirements:
1) where each digit numbers in each row and each column are not repeated.
2) 3 where each digital
number 3 of the matrix is not repeated.
3) the range of numbers to be filled is an integer from 1-9
the following Source: http: //sudoku.baike.com/article-75231.html
Sudoku 谜面Sudoku mystery

Sudoku rules are not complicated, from the perspective of the algorithm, we can think Sudoku implementations from two perspectives:
First, the Sudoku riddle generate;
Second, given Sudoku crack the riddle of
this article to discuss a point of view, that is, riddle of generation, follow-up will continue to discuss the perspective of thinking Sudoku triggered.
Next we discuss how to generate Sudoku riddle.
We can know, Sudoku matrix can be seen as a small matrix 3 * 3 to 9 units of small digital matrix composed of the same small matrix numbers are not repeated. If we each row or each column of the matrix small as a unit, the unit where the row or column of the matrix can in fact be seen as a small additional rows or columns of permutations and combinations.
As shown below:
Sudoku a schematic exploded .png
Of course, this is a not too rigorous method, in fact, only the number of the numbers in a matrix of rows and not in small units of columns or (3 digits as one unit) of the unit must be fixed limit, thus we generated this Sudoku method in fact only 9! Species may generate Sudoku riddle.
This method does not generate Sudoku generate all possible Sudoku riddle, but the generated Sudoku Sudoku riddle is to meet the rules, follow-up we will explore other ways to generate a separate number.
Based on the law and rules of Sudoku we found, we can generate Sudoku in the following manner.

1)随机生成一个3*3的数字矩阵,以该矩阵作为中心矩阵
2)以中心矩阵的行为单位,交换行,要求每一行9个数字满足不重复规则,生成中心矩阵的左右两个3*3矩阵
3)以中心矩阵的列为单位,交换列,要求每一列9个数字满足不重复规则,生成中心矩阵的上下两个3*3矩阵
4)以步骤 3)生成的矩阵为依据,继续以行为单位,交换行,生成剩下的矩阵

The following is an example of map algorithm ideas:
Sudoku generate 1.png
Sudoku generate 2.png

Sudoku generate 3.png

Sudoku generate 4.png
Code implementation:
https://github.com/Belingda/SudokuHtmlGame/blob/master/RealNum.py

Published 13 original articles · won praise 0 · Views 1018

Guess you like

Origin blog.csdn.net/Belingda/article/details/104714728