n Queen's problem recursive solution 2

operation result:

 

 

code show as below:

1 #include <bits / stdc ++. H>
 2  using  namespace std;
 3  const  int MAX = 1024 ;
 4  const  char * LINE32 = " --------------------- ----------- " ;
 5  const  bool PRINT_DETAILS = false ; 
 6  long  long n, cnt = 0 ;     // n represents the number of queens, cnt represents the number of solutions 
7  int vis [ 3 ] [ 2 * MAX + 1 ];     //v [0] [], v [1] [], v [2] [] indicate whether there is a queen in the column, main diagonal, and sub-diagonal.
 8                          // When 0, it means there is no queen, and non-zero, it means
      Yes , and v [0] [4] = 2 means there is a queen in the second row of the fourth column 9  
10  void print () {
 11 cout << LINE32 << endl;
 12      cout << " " << cnt << " A solution: " << endl;
 13      for ( int i = 1 ; i <= n; i ++ ) {
 14          if (i! = 1 ) {
 15              cout << " , " ;
16         }
17         cout << "(" << vis[0][i] << "," << i << ")";
18     } 
19     cout << endl;
20     for (int i = 1; i <= n; i++) {
21         for (int j = 1; j <= n; j++) {
22             if (vis[0][j] != i) {
23                 cout << 'x';
24             } else {
 25                  cout << ' Q ' ;
 26              }
 27          }
 28          cout << endl;
 29      }
 30  }
 31  
32  bool check ( int row, int col) {     // Check whether it can be at the (row, col) coordinate Place Queen 
33      return ! (Vis [ 0 ] [col] || vis [ 1 ] [row-col + n] || vis [ 2 ] [row + col]);
 34  }
 35  void place (int row) {             // Place queen on row row 
36      if (row> n) {
 37          cnt ++ ;
 38          if (PRINT_DETAILS) {
 39              print ();
 40          }
 41      } else {
 42          for ( int col = 1 ; col <= n; col ++ ) {
 43              if (check (row, col)) {
 44                  vis [ 0 ] [col] = row;
 45                  vis [ 1 ] [row-col + n] = vis [ 2] [row + col] = 1 ;
 46                  place (row + 1 );
 47                  vis [ 0 ] [col] = vis [ 1 ] [row-col + n] = vis [ 2 ] [row + col] = 0 ;
 48              }
 49          }
 50      }
 51  } 
 52  
53  int main () {
 54      // input 
55      cout << " Enter the number of queens: " ; 
 56      cin >> n;
 57      // compute 
58     clock_t begin = clock (); 
 59      place ( 1 );
 60      clock_t end = clock (); 
 61      // output 
62      cout << LINE32 << endl;
 63      cout << n << " Queens have a total of " << cnt << "kind of solution " << endl; 
 64      cout << LINE32 << endl;
 65      cout << " recursive solution 2 solution " << n << " Queens problem takes time " <</*end-begin << "打点" <<*/(double)(end-begin)/CLOCKS_PER_SEC  << "s" << endl;
66     return 0;
67 } 
68 // 14 3~4s

 

Compared with recursive solution 1:

①The operation of recursive solution 1:

 ②The operation of recursive solution 2:

It can be seen that the running time of solution 1 is about three times that of solution 2. It is not difficult to see the reason: although the space efficiency of solution 1 is high, the impact is that it must be cycled when the check method is judged ; and solution 2 uses more More space can directly determine whether the location meets the requirements.

 

Guess you like

Origin www.cnblogs.com/realize1536799/p/12728373.html