1. What is Queen N?
Place n queens that are not under attack on the chessboard of n × n grids. According to the rules of chess, the queen can attack pawns in the same row, column, or diagonal line. The problem after n is equivalent to placing n queens on the n × n board. Any two queens may wish to be on the same row, column, or diagonal line.
Ii. Algorithms
1. Place the first queen in the first space in the first line
2. For the second line, search for spaces that do not conflict with the Queen of the first line from the first space. The first non-conflicting space found is 2nd.
3. For the third row, no space is found that does not conflict with the two queens that were previously placed. Restore the current row to its initial state and return it to the previous row.
4. Search for a position not in conflict with the former Queen after the space occupied by the current Queen. In either case, move the queen of the current row to this location and process the next row. If no proper position is found for the last space until the last row ends, the current row is restored to the initial state and continues to be traced back to the previous row.
5. successfully placing the last queen in the last row represents a feasible solution. Return to step 4.
6. When you need to trace back to 0th rows (outside the table), it indicates that all possible feasible solutions have been searched.
3. 4 Queen's Solutions
Step 1
Step 2
Step 3: find that there is no proper position in the third row, so move the Queen's position in the second row to the next non-conflicting position.
Step 4
Step 5 finds that the fourth row does not have a proper position, so it goes back to the second row and finds that the second row is in the beginning, so it goes back to the first row. Move back the Queen's position in the first line.
Step 6
Step 7
4. Procedures
# Include "stdafx. h" # include
Using namespace std; int n = 0; int arr [100]; bool place (int I) // determines whether the nth queen can be placed {for (int j = 0; j
= 0) {while (! Place (k) & k
> N; if (n =-1) break; int x = queue (n); cout <
V. number of answers to Queen N's questions
N solution (n)
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
11 2680
12 14200
13 73712
14 365596
15 2279184
16 14772512
17 95815104
18 666090624
19 4968057848
20 39029188884
21 314666222712
22 2691008701644
23 24233937684440
24 227514171973736
25 2207893435808352
The above results can be used as test case verification procedures.