代码不懂可以看注释,还是较容易理解的,利用flag可以防止不必要的递归。
#include#include int P[10]={0}; //此列的皇后为第几行 int n = 4; //几皇后 bool hashTable[10]={false}; //此行没皇后 void generateP(int index){ if(index == n+1){ for(int i=1;i<=n;i++){ printf("%d", P[i]); //输出结果,按列,如果想按行输出可以倒过来输出本数组 } printf("n"); return ; } for(int x=1;x<=n;x++){ //第x行没有皇后 if(hashTable[x] == false){ bool flag = true; for(int pre=1;pre



