1 #include <iostream>
2 using namespace std;
3
4 int ans=0;
5 const int N = 8;
6 int a[N][N]={0};
7
8 void show(){
9 cout<<"answer:"<<ans<<endl;
10 for(int i=0;i<N;i++){
11 for(int j=0;j<N;j++){
12 cout<<a[i][j]<<' ';
13 }
14 cout<<endl;
15 }
16 cout<<endl;
17 }
18
19 bool check(int row,int col){
20 int i,j;
21 if(!row) return true;
22 for(i=row-1;i>=0;i--){//检查同一列上面的每一行
23 if(a[i][col]) return false;
24 }
25 for(i=row-1,j=col-1;i>=0&&j>=0;i--,j--){//检查左上
26 if(a[i][j]) return false;
27 }
28 for(i=row-1,j=col+1;i>=0&&j<=N-1;i--,j++){//检查右上
29 if(a[i][j]) return false;
30 }
31 return true;
32 }
33
34 void func(int row){
35 for(int j=0;j<N;j++){
36 a[row][j]=1;
37 if(check(row,j)){
38 if(row==N-1){
39 ++ans;show();
40 }else{
41 func(row+1);
42 }
43 }
44 a[row][j]=0;
45 }
46 }
47 int main(){
48 func(0);
49 return 0;
50 }