N queen's problem solutions and number of solutions

Source: Internet
Author: User

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

Q

Step 2

Q

Q

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.

Q

Q

Step 4

Q

Q

Q

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.

Q

Q

Step 6

Q

Q

Q

Step 7

Q

Q

Q

Q



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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.