HDOJ 2553 N queen question (Classic backtracking)

Source: Internet
Author: User

HDOJ 2553 N queen question (Classic backtracking)
N queen's question

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 12904 Accepted Submission (s): 5811


 

Problem Description places N queens on the square board of N * N so that they do not attack each other (that is, two queens are not allowed to be in the same row, the same column, it is not allowed to be on a diagonal line with 45 corners of the checker border.
Your task is to determine the number of valid placement methods for the given N.


There are several Input rows. Each row has a positive integer of N ≤ 10, indicating the number of the Board and the Queen. If N = 0, it indicates the end.
There are several rows in Output. Each row has a positive integer, indicating the number of different places corresponding to the queen of the input row.
Sample Input
1850

Sample Output
19210


Difficult, difficult )... Baidu has a piece of code, which can only be understood for more than an hour. Is it because the IQ is too low ..........

To help more students understand the problem, I try my best to write it in detail.

 

This section describes how to calculate the diagonal line. X-y calculates the primary diagonal line x + y calculates the secondary diagonal line

For example: () (,) () the x-y values of these points are equal, so they can be connected to the primary diagonal. Similarly, the points x-y () are also equal, and can be connected to the main diagonal.

() And (), () The x + y of these points are equal, and these points can be connected into a diagonal line.

 

 

 

Solution: The n queen problem is to consider the location of the Queen. For each row, we need to enumerate the location where each queen can be placed. We need to judge the current location (I

Line) whether the condition is met, that is, to determine whether this location and the placement of the front I-1 of the Queen's position conflict, if the conflict, this position is not suitable; then jump to the next column

(Note: it is a column). If it is a conflict, continue to the next column until the last column. If the last column cannot be placed, it indicates that the placement method is incorrect, return to the previous emperor

Move back to the next column. Otherwise, you can enumerate the location of the Queen in the next row until the nth row.

 

The Code is as follows:

 

# Include
 
  
Int num, sum [11], map [11], n, x; void dfs (int x) {int I, j, sign; if (x = n + 1) // when the number of placed queens exceeds n, the number of feasible solutions plus 1 num ++; else {for (I = 1; I <= n; I ++) {sign = 1; map [x] = I; // place the x queen in column I for (j = 1; j
  
 

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.