The eight queens question is a question with the background of chess: how can we place eight queens on the 8x8 chess board, so that no queen can directly eat any other queen? For this purpose, neither queen can be in the same horizontal, vertical, or diagonal line. The eight queens problem can be promoted to the more general n queens placement problem: the size of the Board becomes n × n, and the number of Queens also becomes n. If and only when n =
When 1 or N is greater than or equal to 4, the problem is resolved.
Place n queens on the chessboard of n × n grids so that they cannot attack each other. That is, neither of the two queens can be in the same row, column, or diagonal line, solve the Board layout that meets the conditions.
The N-queen problem is a typical problem that can be solved using the backtracking algorithm. If you understand the specific execution process of the problem, you will have a certain understanding of the characteristics of the problem, and then select an appropriate algorithm to solve it.
Take the 8-queen problem as an example. Assume that the row where the queen is located is represented by the variable row, and the corresponding column is represented by the array column [row.
The process of using the backtracking algorithm is as follows:
(1) Place 1st Queens for the first time
Place the 1st queens in The 0th rows and 0th columns, that is, row = 0, column [row] = column [0],:
1st queens are placed at coordinates (0, 0.
(2) Place 2nd Queens for the first time
Because 1st queens have been placed, 1st queens are placed in 0th rows and 0th columns, starting from the lattice below 1st queens. Row = 1:
When Column [row] = column [1] = 0, it is in conflict with the 1st queens in the same column. Continue and move to the next column (that is, column [row] ++ );
When Column [row] = column [1] = 1, it is in conflict with the 1st queens on the same diagonal line, move to the next column (that is, column [row] ++ );
When Column [row] = column [1] = 2, 1st queens are placed on the same row, column, and diagonal line as the 2nd queens.
:
2nd queens are placed at coordinates (1, 2.
(3) Place 3rd Queens for the first time
Because 2nd queens have been placed, 2nd queens are placed in 1st rows and 2nd columns, starting from the lattice below 2nd queens. Row = 2:
When Column [row] = column [2] = 0, it is in conflict with the 1st queens on the same diagonal line, move to the next column (that is, column [row] ++ );
When Column [row] = column [2] = 1, it is in conflict with the 2nd queens on the same diagonal line, move to the next column (that is, column [row] ++ );
When Column [row] = column [2] = 2, it is in conflict with the 2nd queens in the same column. Continue and move to the next column (that is, column [row] ++ );
When Column [row] = column [2] = 3, it conflicts with the 2nd queens on the same diagonal line, move to the next column (that is, column [row] ++ );
When Column [row] = column [1] = 4, 1st queens are placed on the same row, column, and diagonal line as 3rd and two queens.
:
3rd queens are placed at coordinates (2, 4.
(4) Place 4th Queens for the first time
:
4th queens are placed at coordinates (3, 1.
(5) Place 5th Queens for the first time
:
4th queens are placed at coordinates. And so on.
The Code is as follows:
Remarks: Reproduced in http://hi.baidu.com/shirdrn/blog/item/2720311b5cc970108618bfb1.html