Analysis and Realization of eight queens problem


Eight queens problem

Eight queens problem is an old and famous problem, which is a typical case of backtracking algorithm. In 1848, the chess player Max Bessel put forward the question: put eight queens on the 8 × 8 space chess, so that they can’t attack each other, that is: any two Queens can’t be in the same row, the same column or the same diagonal line, ask how many kinds of pendulum.

Analysis and Realization of eight queens problem Analysis and Realization of eight queens problem

Analysis of eight queens problem algorithm

  1. The first queen, first row, first column
  2. Put the second queen in the first column of the second row, and then judge whether it is OK. If it is not OK, continue to put it in the second column, the third column, and put all the columns in turn to find a suitable one
  3. Continue with the third queen, or the first and second columns Until the eighth queen can also be placed in a non conflict position, it is found a correct solution
  4. When we get a correct solution, when the stack goes back to the previous stack, we will start backtracking, that is, all the correct solutions of the first queen in the first column will be obtained
  5. Then go back to the first queen and put it in the second column, and then continue to cycle through steps 1, 2, 3 and 4


Theoretically, a two-dimensional array should be created to represent chessboard, but in fact, a one-dimensional array can be used to solve the problemarr[8] = {0 , 4, 7, 5, 2, 6, 1, 3}//The corresponding arr subscript indicates the row, that is, the queen,arr[i] = val , valDenotes the secondi+1A queen, put it in the second placei+1The second of the linesval+1column

Using backtracking algorithm

Gauss thinks there are 76 options. In 1854, different authors published 40 different solutions in the chess magazine in Berlin. Later, 92 results were obtained by using graph theory. After the invention of computer, there are many kinds of computer language can solve this problem

package  com.atguigu.recursion ;

When judging the oblique line, subtract the ordinate from the abscissa directly. If the difference between the two positions is equal,

It’s on the same slash

However, in the above example, the storage method is as follows:

One dimensional array: the subscript of the array represents the line number of the chessboard, and the value of the array represents the column number of the chessboard

The number of members in the array is the Queen’s chess pieces

When judging the memory slash, we calculate whether the horizontal difference and the vertical difference of the two points are equal. If they are equal, then the slope is 1, that is, tan45 degrees. Well, we can judge that it is on a slash, and the Queens can attack each other, um, oh, but seconds

Analysis and Realization of eight queens problem

This work adoptsCC agreementReprint must indicate the author and the link of this article

Autumn leaves and summer wind