Problem description
The eight queens question was raised by the famous mathematician Gauss in the 19th century in 1850. The problem is: Eight queens are placed on the 8*8 board, so that they cannot attack each other. That is, the two queens cannot be in the same row, column, or diagonal line of consent. The eight queens problem can be extended to the N queens problem, that is, there are n queens on the N * n chessboard, so that any two queens cannot be on the same row, column, or diagonal line.
Problem Analysis
We analyze the four queens Problem in the simplest way. Obviously, in order to prevent the queens from attacking each other, we first consider that each row can only have one queen. We use X [, 3 .... N] represents the array of solutions to this problem. X [N] represents a queen in column X [N] of row N. For example, X [2] = 1 indicates that there is a queen in Column 2nd of row 1st. then, when will the Queen attack each other on the X [k?
Problem Solving
General description: Taking the four queens as an example, we use a table to describe the process of placing the Queens:
As shown above, once there is a situation where the queen cannot be placed in all positions of the bank, we will go back to the previous line and re-adjust the location of the Queen in the previous line.
Interpretation of pseudocode
The first is to determine whether the Queen's code can be placed:
Next is the process of placing the queen:
Backtracking -- solving the n queen problem