void EightQueen(int *path,int k,int* count) { if (k==8) { for (int i=0;i<8;i++) { printf("(%d,%d) ",i,path[i]); } printf("\n"); (*count)++; return ; } for (int i =0 ;i<8;i++) { bool flag_2=true; //判断列 for (int j=0;j<k;j++) { if (path[j]==i) { flag_2 = false; break; } } //判断对角线 for (int j=0;j<k;j++) { if (abs(j-k) == abs(path[j]-i)) { flag_2 = false; break; } } if (flag_2) { path[k] = i; EightQueen(path,k+1,count); } } }
main函数为:
int count=0; int *path = new int[100]; memset(path,0,400); EightQueen(path,0,&count); printf("%d\n",count);