in the course of studying the construction of modern software engineering, the teacher asked to publish a blog, using the Java algorithm to achieve the eight Queen problem solved. When I wrote this blog, I learned some other blogs, because I often encountered problems, I can not solve, to learn from others is also a way.
International chess player Max Bessel in 1848: Put eight queens on the 8x8 chess, so that they can not attack each other, that is, any two queens can not be in the same row, the same column or the same slash, ask how many kinds of pendulum.
We can do the traversal of the feasible placement scheme by row or column, each row (or column) traverses a qualifying position, and then goes to the next row or column to traverse the appropriate position of the next piece, which ensures that there is a condition in the traverse process that is absolutely consistent-- The next piece is placed in the same row (or column) as the previous piece. Next, we just have to determine if the current position meets other criteria, and if so, traverse the next row (or column) all the places, see if there is a continuation of the qualifying position, and so on, if all the locations of a row (or column) are not appropriate, return to the previous row (or column) to continue the other position of the row When we successfully traverse to the last row (or column), and there is a qualifying position, it is a feasible 8 queen placement scheme, accumulate the number of the eight Queen feasible scheme, and then continue to traverse the row where the other position is appropriate, if not, then return to the previous row, traverse the other position of the line, then go on. With this process in place, we can draw all the 8 queens that meet the criteria. This is the idea of recursion.
Next, we walk through the columns, specific to the code, for further explanation. First, starting from the first column to find the right position of the first piece, we know that at this time any position in the first column is appropriate, when the pieces find the first suitable position, then start to the next column to consider the next suitable position, at this time, the second column of the first row and the second row obviously can not put a second piece, Because it is the same line as the first piece, one on the same slash. The third row of the second column becomes the first suitable position in the second column, and so on, the 5th row of the third column will be a suitable position, and in this process, we notice that the appropriate position of each column is affected by the position of the previous columns, assuming that the first 1 columns of the pieces are placed on the 3rd row, the current column cannot be placed 2 lines, 4 rows. For example, the cols array is used to represent the number of rows of 8-column pieces, the array subscript starts with 0, where the array subscript indicates the number of columns, the element value of the array represents the number of rows in the column, and the currently listed N (N>=0,N<8):
Cols[n]! = cols[n-1]
Cols[n]! = cols[n-1]-1
Cols[n]!=cols[n-1]+1
If the N-2 column exists, then we also have to consider that the current column n does not go with the N-2 column, the same slash, with the backslash. Set the previous column of the current column N to M, then all the values for M are set to {M>=0,m<n}:
cols[n]! = Cols[m]
cols[n]! = cols[m]-(N-M)
cols[n]! = Cols[m] + (N-M)
The specific code is as follows:
public class Queen8{public static int num=0;public static final int maxqueen=8;public static int[] col=new Int[maxqueen];p Ublic Queen8 () {getarrangement (0); System.out.print ("/n"); System.out.println (maxqueen+ "Eight queens question has" +num+ "kind of arrangement method. ”);} public void getarrangement (int n) {boolean[] rows=new boolean[maxqueem];for (int i=0;i =0) rows[cols[i]-d]=true;if (cols[i]+d<=maxqueen-1) rows[ Col[i]+d]=true;} for (int i=0;i Eight Queen (Java algorithm implementation)