Place eight queens on an 8*8 chessboard, so that they are not on a horizontal or vertical or diagonal line. You can imagine that the first piece occupies the first position () on the first board, and then places the second piece on the second Board to meet the conditions of the Queen, for example, (2, 3. When there is no position available, you must move the position of the pawns in the previous line, that is, to trace back. For other details, refer to relevant books, it can be recursive or non-recursive. I hope it is clear :)
---------------------------------------------------------------
It is best to use the stack to solve this problem:
1. Create a stack first. Then, put the first pawn in the first row, the first column (), and store it to the bottom of the stack, as described above.
2. traverse the position of the second piece and place the second piece in the position where the second row of the Board meets the conditions, such as (2, 3), and save it to the top of the stack.
3. Search for the position of the Third pawn,
4 ,............
5. When the position of the previous piece cannot be found, the position of the previous piece is incorrect. Take the previous piece out of the stack and traverse it to find other positions of the previous piece,
6. If the position of the previous pawn is not found, continue to the stack and traverse to find its position,
7 ,......
Actually, this is a recursive and rollback problem, but in the process of recursion and backtracking, stack is used to store the position of the piece found.