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