1 #include <set> 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <cmath> 6 using namespace std; 7 8 9 int X[8]; 10 int c=0; 11 bool place(int k) 12 { 13 int i; 14 for (i=0;i<k;i++) 15 { 16 if (X[i]==X[k]||abs(X[i]-X[k])==abs(i-k)) 17 { 18 return false; 19 } 20 } 21 return true; 22 } 23 void printSol(int n) 24 { 25 int i; 26 for (i=0;i<n;i++) 27 { 28 cout<<X[i]+1<<" "; 29 } 30 cout<<endl; 31 c++; 32 } 33 void NQueens(int n) 34 { 35 int k=0; 36 X[0]=-1; 37 while (k>=0) 38 { 39 X[k]++; 40 while (X[k]<8&&place(k)==false) 41 { 42 X[k]++; 43 } 44 if (X[k]<n) 45 { 46 if (k==n-1) 47 { 48 printSol(n); 49 } 50 else 51 { 52 k++; 53 X[k]=-1; 54 } 55 } 56 else 57 { 58 k--; 59 } 60 61 } 62 } 63 int main() 64 { 65 NQueens(8); 66 cout<<c<<endl; 67 }