operation result:
code show as below:
1 #include <bits / stdc ++. H> 2 using namespace std; 3 #include <bits / stdc ++. H> 4 using namespace std; 5 const int MAX = 1024 ; 6 const char * LINE32 = " ------ -------------------------- " ; 7 const bool PRINT_DETAILS = false ; 8 long long n, cnt = 0 ; // n is the number of queens , Cnt represents the number of solutions 9 int queen [MAX + 1]; 10 11 void print() { 12 cout << LINE32 << endl; 13 cout << "第" << cnt << "个解法: " << endl; 14 for (int i = 1; i <= n; i++) { 15 if (i != 1) { 16 cout << ", "; 17 } 18 cout << "(" << i << "," << queen[i] << ")"; 19 } 20 cout << endl; 21 for (int i = 1; i <= n; i++) { 22 for (int j = 1; j <= n; j++) { 23 if (queen[i] != j) { 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 queen can be placed at the (row, col) coordinate 34 for ( int placed = 1 ; placed < row; placed ++ ) { 35 if (queen [placed] == col || abs (row-placed) == abs (col- queen [placed])) { 36 return false ; 37 } 38 } 39 return true; 40 } 41 void solve ( int n) { 42 int row = 1 ; // row indicates the row currently being placed, the initial value is 1, indicates that the queen is placed from the first row 43 queen [row] = 1 ; // That is, a [1] = 1 means that at the beginning, put a queen in (1, 1) 44 while (row> 0 ) { // row value is finally 0, because when all conditions are checked, the first row Backtracking upwards, the row value is 0 45 if (row> n) { 46 // Find a solution, and then need to backtrack upwards: check from the next column of the previous line 47 cnt ++ ; 48 if (PRINT_DETAILS) { 49 print (); 50 } 51 queen [-row] ++ ; 52 } else if (queen [row]> n) { 53 // queen [row] indicates the position of the column to be detected on the current row 54 // At this point, all positions of the row row have been checked, and the same upward traceback: check from the next column of the previous row 55 queen [-row] ++ ; 56 } else if (check (row, queen [row] )) { 57 // Find a matching position, start to place the next row, starting from the first column 58 queen [++ row] = 1 ; 59 } else { 60 // This time indicates that this position does not match, check the position of the next column in this row 61 queen [row] ++ ; 62 } 63 } 64 } 65 66 int main () { 67 // input 68 cout << " Enter the queen Number: " ; 69 cin >> n; 70 // compute 71 clock_t begin = clock (); 72 solve (n); 73 clock_t end = clock (); 74 // output 75 cout << LINE32 <<endl; 76 cout << n << " Queen's problem in all " << cnt << " kinds of solutions " << endl; 77 cout << LINE32 << endl; 78 cout << " Non-recursive solution 1 solution " < <n << " Queen's Question Time-consuming " << / * end-begin << "Dotted" << * / ( double ) (end-begin) / CLOCKS_PER_SEC << " s " << endl; 79 return 0 ; 80 } 81 // 14 9~11s
Compared with recursive solution 1:
①The operation of recursive solution 1:
②The operation of non-recursive solution 1:
You can see, non-recursive solution 1 run slightly more than 1 time a number of recursive solution, which is not in line with expectations , logically speaking, non-recursive solution running time should be shorter next blog post will compare non-recursive and recursive solution 2 Solution 2 .