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 ]; 8 //v [0] [], v [1] [], v [2] [] represent the column, the main diagonal, if there are sub-diagonal Queen 9 @ 0 means no queen, non zero indicates Yes , and v [0] [4] = 2 means there is a queen in the second row of the fourth column 10 11 void print () { 12 cout << LINE32 << endl; 13 cout << " 第" << cnt << " Solutions: " << endl; 14 for ( int i = 1 ; i <= n; i ++ ) { 15 if (i! = 1 ) { 16 cout << " , " ; 17 } 18 cout << "(" << vis[0][i] << "," << i << ")"; 19 } 20 cout << endl; 21 for (int i = 1; i <= n; i++) { 22 for (int j = 1; j <= n; j++) { 23 if (vis[0][j] != i) { 24 cout << 'x'; 25 } else { 26 cout << ' Q ' ; 27 } 28 } 29 cout << endl; 30 } 31 } 32 33 bool check ( int row, int col) { // Check whether the coordinates can be in (row, col) Place queen 34 return ! (Vis [ 0 ] [col] || vis [ 1 ] [row-col + n] || vis [ 2 ] [row + col]); 35 } 36 void place (int x, int y, int * r2c) { // Place the queen at the position of (x, y) 37 vis [ 0 ] [y] = x; 38 vis [ 1 ] [x-y + n] = vis [ 2 ] [x + y] = 1 ; 39 r2c [x] = y; 40 } 41 void remove ( int x, int y) { // Remove the queen at the (x, y) position 42 vis [ 0 ] [ y] = vis [ 1 ] [x-y + n] = vis [ 2 ] [x + y] = 0 ; 43 } 44 void solve ( int n) { // The difference with non-recursive implementation 1 is that A. uses vis [3] [] to speed up the judgment, B. The specific operation of backtracking is on vis [3] [] instead of 45 int on r2c [] r2c [n + 5 ]; // The number of columns where each row is placed is actually similar to queen [] used in recursive implementation 1 and non-recursive implementation 1 46 r2c [ 0 ] = 0 ; // Here to initialize, otherwise the array will be out of bounds when exiting the following loop, the affected code is 63_42 47 int row = 1 , col = 1 ; 48 place (row, col, r2c); 49 row = 2 ; // in Place a queen at the position of (1, 1), then enter the loop and start looking for the position of the second row 50 while (row> 0) { // The value of row is 0 at the end, because when all the conditions are checked, the first line is traced back, the value of row is 0 51 if (row> n) { 52 // Find a solution, and then need to trace back up : Remove the queen from the previous row, try to place the queen from the next column of the previous row 53 cnt ++ ; 54 if (PRINT_DETAILS) { 55 print (); 56 } 57 row--; // row returns the previous row 58 remove (row, r2c [row]); // Remove the queen in the previous row 59 col = r2c [row] + 1 ; // The (row, col) at this time is the position of the next attempt to place 60 } else if(col> n) { 61 // Every position in the current row is tried and placed, backtrack 62 row-- ; 63 remove (row, r2c [row]); 64 col = r2c [row] + 1 ; 65 } else if (check (row, col)) { 66 // Find a matching position 67 place (row, col, r2c); // Place the queen 68 row ++; // Find the position where the next row is placed 69 col = 1 ; // and put 70 from the first column } else { 71 // This column does not match, find the next column 72 col ++ ; 73 } 74 } 75 } 76 77 int main () { 78 // input 79 cout << " Enter the number of queens: " ; 80 cin >> n; 81 / / compute 82 clock_t begin = clock (); 83 solve (n); 84 clock_t end = clock (); 85 // output 86 cout << LINE32 << endl; 87 cout << n << " Queen's problem in all " << cnt << " kinds of solutions " << endl; 88 cout << LINE32 << endl; 89 cout << " Backtracking non-recursive algorithm implementation 2 solution " << n << " Queen's question is time-consuming " << / * end-begin << "Managing" << * / ( double ) (end-begin) / CLOCKS_PER_SEC << " s " << endl; 90 return 0 ; 91 } 92 // 14 3 ~ 5s
Comparison with backtracking algorithm implementation 2
Backward recursive algorithm to achieve 2 operating conditions
Backtracking non-recursive algorithm to achieve 2 operating conditions
The non-recursive implementation is still a bit slower than the recursive implementation, which is a bit unpredictable.