题目传送:洛谷P1219八皇后
dfs
#include<iostream>
using namespace std;
int n,ans=0;
int vis[3][50];
int col[50];
void dfs(int row){
if(row==n){
ans++;
if(ans<=3){
for(int i=0;i<n-1;i++)
cout<<col[i]+1<<' ';
cout<<col[n-1]+1<<endl;
}
}
for(int i=0;i<n;i++){
if(!vis[0][i]&&!vis[1][row-i+n]&&!vis[2][row+i]){
col[row]=i;
vis[0][i]=vis[1][row-i+n]=vis[2][row+i]=1;
dfs(row+1);
vis[0][i]=vis[1][row-i+n]=vis[2][row+i]=0;
}
}
}
int main(){
cin>>n;
dfs(0);
cout<<ans<<endl;
return 0;
}