代码如下:
1 #include <iostream> 2 #include <cmath> 3 #include <memory.h> 4 using namespace std; 5 6 int N = 0; 7 int queenarr[100]; 8 int queen_count = 0; 9 10 int queensolt(int k) 11 { 12 //int static queen_count = 0; 13 if(k == N) 14 { 15 for (int i = 0; i < N; ++i) 16 { 17 cout << queenarr[i] << " "; 18 } 19 cout << endl; 20 return (++queen_count); 21 } 22 23 for(int i = 0;i < N;++i) 24 { 25 int j; 26 for(j = 0;j < k;++j) 27 { 28 if(queenarr[j] == i || abs(queenarr[j]-i) == abs(k-j)) 29 break; 30 } 31 if(j == k) 32 { 33 queenarr[k] = i; 34 queensolt(k+1); 35 } 36 } 37 return queen_count; 38 } 39 40 int main(int argc,char* argv[]) 41 { 42 cin >> N; 43 memset(queenarr,0,sizeof(queenarr)); 44 45 cout << "Result:" << queensolt(0) << endl; 46 47 return 0; 48 }