#pragma once
#include <stdio.h>
#include <math.h>
#define len 8
int m = 0;
void printp(int p[len][len]) {
printf("ponit:\n");
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
printf("%d ", p[i][j]);
}
printf("\n");
}
}
int isCan(int row, int colume,int p[]) {
for (int i = 0; i < row; i++) {
if (p[i] == colume) return 0;
if (abs(row-i) == abs(colume-p[i])) return 0;
}
return 1;
}
void run(int row,int p[]) {
for(int j =0; j <len; j++){
if (isCan(row,j,p)) {
p[row] = j;
if (row == len - 1) {
printf("result:\n");
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
printf("%d ", p[i] == j ? 1 : 0);
}
printf("\n");
}
m++;
}
if (row < len - 1) {
run(row + 1, p);
}
}
}
}
void queen_main() {
int p[len];//每一行皇后的坐标
for (int i = 0; i < len; i++) {
p[i] = -1;
}
run(0,p);
printf("解法总数: %d", m);
}
05-03
7617
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-27
08-17
1049
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)