八皇后
传送门:https://www.luogu.org/problemnew/show/P1219
代码
#include<cstdio> using namespace std; int n; int a[100],b[100],c[100],d[100],tot; int print(){ tot++; if(tot <= 3){ for(int i = 1;i <= n;i++) printf("%d ",a[i]); printf("\n"); } } void search(int i)//第几行 { if(i > n){ print(); return; } else{ for(int j = 1;j <= n;j++){ if(!b[j] && !c[i+j] && !d[i-j+n]){ a[i] = j; b[j] = c[i+j] = d[i-j+n] = true; search(i+1); b[j] = c[i+j] = d[i-j+n] = false; } } } } int main() { scanf("%d",&n); search(1); printf("%d",tot); return 0; }