Eight Queens problem - Print Solutions

. 1 #include <cstdio>
 2 #include <Queue>
 . 3 #include <the iostream>
 . 4 #include <Utility>
 . 5 #include <algorithm>
 . 6 #include <Vector>
 . 7  the using  namespace STD;
 . 8  
. 9  int C [ 10 ]; / / temporary recording position of each row of the queen 
10  const  int n-= . 8 ; // Number of queens 
. 11  
12 is  char P [ . 8 ] [ . 8 ]; // record whether the board can drop 
13 is  int N = . 1 ;
14  
15  void ewq ( int I, int X, int Y) // in the xy is around plus I 
16  {
 . 17      for ( int J = 0 ; J <n-; J ++ )
 18 is      {
 . 19          P [X] [J] + = I;
 20 is          P [J] [Y] + = I;
 21 is          IF (X-Y + J> = 0 && n-> X - Y + J)
 22 is          {
 23 is              P [X - Y + J] [J] + = I;
 24          }
 25          IF (YJ + X> = 0&& n > x+y-j)
26         {
27             P[x+y-j][j]+=i;
28         }
29     }
30     P[x][y] -= 3*i;
31 }
32 
33 void pri()//打印解
34 {
35     for (int i = 0; i < n; i++)
36     {
37         for (int j = 0; j < n; j++)
38             printf("%d ", ((C [I] == J)? . 1 : 0 ));
 39          the printf ( " \ n- " );
 40      } 
 41 is      the printf ( " \ n- " );
 42 is  }
 43 is  
44 is  void pri_1 () // second species print 
45  {
 46 is      the printf ( " . No. D% \ n- " , N ++ );
 47      for ( int I = 0 ; I <n-; I ++ )
 48      {
 49          for ( int J =0; j < n; j++)
50             printf("%d ", ((C[j] == i) ? 1 : 0));
51         printf("\n");
52     }
53 }
54 
55 void qwe(int cur)
56 {
57     for (int i = 0; i < n; i++)
58     {
59         if (!P[cur][i])//有位置
60         {
61             C [CUR] = I; // mark position 
62 is              IF (n-CUR == - . 1 )
 63 is              {
 64                  pri_1 ();
 65                  Continue ;
 66              }
 67              ewq ( . 1 , CUR, I); // write the name 
68              QWE ( + CUR . 1 );
 69              ewq (- . 1 , CUR, I); // cancel name 
70          }
 71 is      }
 72  }
 73 is  
74  
75  int main ()
 76 {
77     memset(P, 0, sizeof(P));
78     qwe(0);
79     return 0;
80 }

 

Guess you like

Origin www.cnblogs.com/li136/p/11616004.html