N Queen Placement problem

Source: Internet
Author: User

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 problem-solving ideas: Using DFS. A row of rows, when placed in a reasonable position on the next line of recursion, until the last line. When there is no reasonable position, select the other columns to be placed. have been handed down so recursively. Until all the placement methods have been found. Program code:
#include <stdio.h>inttot,n,n,m,c[ One],b[ One];voidSearchintcur) {    if(cur==N) Tot++; Else  for(intI=0; i<n; i++)        {            intok=1; C[cur]=i;  for(intj=0; j<cur; J + +)                if(c[cur]==c[j]| | cur-c[cur]==j-c[j]| | cur+c[cur]==j+C[j]) {OK=0;  Break; }            if(OK) search (cur+1); }}intMain () { for(n=1; n<=Ten; n++) {tot=0; Search (0); B[n]=tot; }     while(SCANF ("%d", &n) = =1&&N) printf ("%d\n", B[n]); return 0;}

N Queen Placement problem

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.