HDU 2553 n queen problem (backtracking), hdu2553

Source: Internet
Author: User

HDU 2553 n queen problem (backtracking), hdu2553
DFSTime Limit:1000 MSMemory Limit:32768KB64bit IO Format:% I64d & % I64u

Description

There are N queens on the N * N checkboard so that they do not attack each other (that is, two queens are not allowed to be in the same row, in 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.

 

Input

There are several rows in total. Each row has a positive integer N ≤ 10, indicating the number of the Board and the Queen. If N = 0, it indicates the end.

Output

There are several rows in total, 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 question: recursive enumeration (backtracking) is used ). This article uses row-by-row placement. We need to save the valid number of placement methods in advance and set the number of the Queen to 1, 2 ,......, N, you can think that the queen I must be placed in the row I, and then enumerate the position of the first queen for backtracking. If a queen cannot find the position of the queen at a time, return directly, if all the queens can find the placement location, it indicates that there is a pendulum method that meets the requirements, and the number of pendulum methods is enough. Idea: each row can have at most one queen, so a [] is used to represent the row vector. The search starts with the first row vector.
Search progressively by row vector until the end of the last row vector to obtain a placement method, save the pendulum with B.

AC code:
1 # include <iostream> 2 # include <cmath> 3 using namespace std; 4 const int maxn = 11; 5 int B [maxn], a [maxn], sum, n; 6 7 void dfs (int cur) 8 {9 if (cur = n + 1) // recursive boundary, there is a way to set 10 sum ++; 11 else12 for (int j = 1; j <= n; j ++) 13 {14 int OK = 1; 15 a [cur] = j; // try placing the queen of the cur row in column j 16 for (int I = 1; I <cur; I ++) // check whether there is a conflict with the Queen 17 if (a [I] = a [cur] | abs (I-cur) = abs (a [I]-a [cur]) 18 {19 OK = 0; 20 break; 21} 22 if (OK) 23 dfs (cur + 1 ); // if valid, continue recursion 24} 25} 26 27 int main () 28 {29 for (int I = 1; I <= maxn; I ++) 30 {31 sum = 0; 32 n = I; 33 dfs (1); 34 B [I] = sum; 35} 36 while (cin> n & n) 37 cout <B [n] <endl; 38 return 0; 39}

 

I accidentally found a simple and opportunistic method .... Because n <= 10. But not ac.
1 #include <cstdio>2 main()3 {4     int n,a[13]={0,1,0,0,2,10,4,40,92,352,724,2680,14200};5     while(scanf("%d",&n))6         printf("%d\n",a[n]);7 }

 

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.