HDU 2553 n queen question

Source: Internet
Author: User
N queen's question

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 5300 accepted submission (s): 2409

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

Authorcgf

Source2008 hznu Programming Contest

Recommendlcy

The key is to solve the diagonal line problem.

Take 8*8 As An Example
Number of columns-number of rows (I-j)

Main diagonal line

Number of columns + number of rows (I + J)


Diagonal Line

The numbers on the diagonal lines are equal. Therefore, each diagonal line can be marked with a variable to record the state. For example, an N * n mesh is marked with an array of 2 * n-1 in size. If the coordinate of the current grid is column J of the I row. Ing to the primary diagonal is J-I. But the array subscript cannot be negative. Therefore, if we add n-1, it is converted to a non-negative number. For example, the bottom left to the top right of the Left graph is. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14. Directly map the sub-diagonal lines .. In this way, it is much easier to judge whether the same diagonal line is used. Next, record the answer to this question. If the calculated data is output directly, the answer will not time out.

# Include <stdio. h> # include <string. h> int L [29], R [29], row [15], ANS [15], sum, N; // L indicates the primary diagonal. R indicates the diagonal line of a pair. Whether the row flag column is accessed void DFS (INT p) // P indicates the number of rows to be placed in the Queen {int I; If (P = N) // if each row has a Queen (from 0 to n-1) {sum ++; // The number of methods plus 1 return;} for (I = 0; I <N; I ++) // search for columns {If (! Row [I] &! L [I-P + n-1] &! R [I + P]) // determines the column. The diagonal line of the primary key. Indicates whether the Sub-diagonal has a queen {row [I] = 1; L [I-P + n-1] = 1; // see the preceding description. R [I + P] = 1; DFS (p + 1); // continue the deep search row [I] = 0; // trace L [I-P + n-1] = 0; R [I + P] = 0 ;}} int main () {memset (ANS,-1, sizeof ans); While (scanf ("% d ", & N), n) {If (ANS [N]! =-1) {printf ("% d \ n", ANS [N]); continue;} memset (L, 0, sizeof L); memset (R, 0, sizeof R); memset (row, 0, sizeof row); sum = 0; DFS (0); // search ans [N] = sum from 0; printf ("% d \ n", sum);} return 0 ;}

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.