八皇后
#include<stdio.h>
#include<math.h>
int count = 0;
const int max = 8;
int chessBoard[max];
int isDanger(int row){
for(int i = 0; i < row; i++){
if(chessBoard[i] == chessBoard[row] || abs(row-i) == abs(chessBoard[row]-chessBoard[i]))
return 0;
}
return 1;
}
void print(int chessBoard[]){
for (int i = 0; i < max; i++) {
printf(chessBoard[i]+" ");
}
count ++ ;
printf("\n");
}
void playChess(int row){
if(row == max){
print(chessBoard);
return;
}
for(int i = 0; i < max; i++){
chessBoard[row] = i;
if(isDanger(row) == 1)
playChess(row+1);
}
}
void main(){
playChess(0);
printf("共有%d种方法",count);
}