HDU 2553 N Queen problem (backtracking method)

Source: Internet
Author: User

DfsTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U

Description

N-Queens are placed in the N*n's checkered checkerboard, making them not attack each other (that is, any 2 queens are not allowed to be in the same row, in the same column, or in a diagonal line with a 45-angle checkerboard border.)
Your task is to find out how many legal placement methods are available for a given n.

Input

There are several lines, one positive integer n≤10 per line, indicating the number of boards and queens, or, if n=0, the end.

Output

A number of rows, one positive integer per line, representing the number of different placements of the queen corresponding to the input row.

Sample Input

1850

Sample Output

19210 the problem: The Recursive enumeration method (backtracking method) is used. The subject is placed row by line.to preserve the number of legal placement methods beforehand .In order to set the Queen's number to 1,2,......,n, it can be assumed that the Empress I must be placed in line I, and then enumerate the position of the first queen to backtrack, if one time found that a queen can not find the position of the direct return, if all the Queen could find the position, it means there is a pendulum to meet the requirements, Statistics how many kinds of pendulum can be. Idea: There can be at most one queen per line, so use a[] to represent a row vector, and the search starts with the first line vector
Incremental search by row vectors, until the end of the last line vector to get a placement method, using b[] to save the pendulum method.

AC Code:
1#include <iostream>2#include <cmath>3 using namespacestd;4 Const intmaxn= One;5 intB[maxn],a[maxn],sum,n;6 7 voidDfsintcur)8 {9     if(cur = = n+1)//recursive boundary, there is a pendulum methodTensum++; One     Else A          for(intj =1; J <=n; J + +) -         { -             intok=1; theA[cur] =j;//tried to put the Queen of cur in column J. -              for(inti =1; i<cur; i++//Check if there is a conflict with the Queen in front -                 if(A[i] = = A[cur] | | ABS (I-CUR) = = ABS (a[i)-A[cur])) -                 { +ok=0; -                      Break; +                 } A             if(OK) atDFS (cur+1);//if lawful, continue to recursion -         } - } -  - intMain () - { in      for(inti =1; I <=maxn; i++) -     { tosum =0; +n=i; -Dfs1); theB[i] =sum; *     } $       while(Cin>>n &&N)Panax Notoginsengcout<<b[n]<<Endl; -     return 0; the}

Accidentally found a super-simple, opportunistic way ...
1#include <cstdio>2 Main ()3 {4     intn,a[ -]={0,1,0,0,2,Ten,4, +, the,352,724,2680,14200};5      while(SCANF ("%d",&N))6printf"%d\n", A[n]);7}

HDU 2553 N Queen problem (backtracking method)

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.