小学生兼职。
#include<stdio.h> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int tot,c[1000],vis[3][1000],n; void search(int cur)//枚举第cur列的皇后的位置,一共有n列 { if(cur==n+1)//如果是枚举第n+1列皇后的位置,说明已经枚举了n列皇后的位置,而且彼此的位置相互不冲突。 tot++; else for(int i=1;i<=n;i++) { //vis[0][i]用来记录第i行是否已经有一个皇后了,vis[0][i]=1表示第i行已经有一个皇后,vis[0][i]=0表示第i行没有皇后 //vis[1][cur+i]用来判断副对角线是否已经有皇后了 //vis[2][cur-i+n]用来判断主对角线是否已经有皇后了 if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+n]) { c[cur]=i; vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=1; search(cur+1); vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=0; } } } int main() { cin >> n; memset(c,0,sizeof(c)); memset(vis,0,sizeof(vis)); search(1); cout << tot; return 0; }