目前仅有代码,图解后续完善(递归,回溯)
#include <stdio.h>
int place[8]={0};
int col[8]={1,1,1,1,1,1,1,1};
int up_diagonal[15]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int sub_diagonal[15]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int sum = 0;
void Queens(int);
void print(void);
int main(void){
Queens(0);
return 0;
}
void Queens(int x){
int i;
for(i = 0 ; i < 8 ; i ++){
if(col[i] == 1 && up_diagonal[x-i+7] == 1 && sub_diagonal[x+i] == 1){
place[x] = i;
col[i] = 0;
up_diagonal[x-i+7] = 0;
sub_diagonal[x+i] = 0;
if(x < 7){
Queens(x+1);
}else{
sum ++;
print();
}
col[i] = 1;
up_diagonal[x-i+7] = 1;
sub_diagonal[x+i] = 1;
}
}
}
void print(void){
printf("第%d种解法:\n",sum);
int line , i , j;
int table[8][8]={0};
for(line = 0 ; line < 8 ; line ++){
table[line][place[line]] = 1;
}
for(i = 0 ; i < 8 ; i ++){
for(j = 0 ; j < 8 ; j ++){
printf("%d ",table[i][j]);
}
printf("\n");
}
printf("\n");
}