//参数row:表示起始行 //参数n:表示列数 //参数(*chess)[8]:表示指向棋盘每一行指针 void EightQueen(int row,int n,int (*chess)[8]){ int chess2[8][8],i,j; for(i = 0;i < 8;i++){ for(j = 0;j < 8;j++){ chess2[i][j] = chess[i][j]; } } if(8 == row){ printf("第 %d 种\n",count+1); for(i = 0;i < 8;i++){ for(j = 0;j < 8;j++){ printf("%d ",*(*(chess2+i)+j)); } printf("\n"); } printf("\n"); count++; }else{ for(j = 0;j < n;j++){ if(notDanger(row,j,chess)){ //判断这个位置是否有危险,如果没有危险,继续往下 for( i = 0;i < 8; i++){ *(*(chess2 + row)+ i) = 0; } *(*(chess2 + row) + j) = 1; EightQueen(row+1,n,chess2); } } } }
int main(){ int chess[8][8],i,j; for(i = 0;i < 8;i++){ for(j = 0;j < 8;j++){ chess[i][j] = 0; } } EightQueen(0,8,chess); printf("总共有%d 种解决方法\n\n",count); return 0; }
int main(int argc, char* argv[]) { int count = EightQueen(); printf("The count of ways to place eight queens: %d.\n", count); }