DFS-C - N queens problem

C - N Queens Problem

In the checkerboard-N * N N queens placed such that they do not attack each other (i.e., does not allow any two queens in the same row, same column, is not allowed in the board frame 45 on a diagonal angle .
your task is, for a given N, how many legitimate method of obtaining placed there.

Input total number of lines, each line a positive integer n ≤ 10, and represents the number of Queen board; if N = 0, indicating the end. Output total number of lines, each line a positive integer representing the number of different placement Queen corresponding input line. Sample Input

1
8
5
0

Sample Output

1
92
10
 
. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3  
. 4  int n-, SUM;
 . 5  int A [ 10 ]; // store each line of the Queen abscissa 
. 6  int B [ 10 ]; // play table 
7  
. 8  int Check ( int X) {
 . 9      for ( int I = 0 ; I <X; I ++ )
 10          IF (A [I] == A [X] || ABS (A [X] -a [I]) = ABS = (XI))     return  0 ;
 . 11      return  . 1 ;
12 }
13 
14 void DFS(int x){
15     for(int i=0; i<n; i++){
16         a[x] = i;
17         if(check(x)){
18             if(x == n-1)    sum++;
19             else            DFS(x+1);
20         }
21     }
22 }
23 
24 int main(){
25     while(~scanf("%d", &n) && n){
26         if(b[n])    printf("%d\n",b[n]);
27         else{
28             sum = 0;
29             DFS(0);
30             b[n] = sum;
31             printf("%d\n",sum);
32         }
33         
34     }
35 }
 

 

 

Guess you like

Origin www.cnblogs.com/0424lrn/p/12232684.html
Recommended