# 八皇后递归求解

bool isConfilctQueen(int **martix, int row, int column, int count) {
bool result = false;
for (int i = 0; i < count; i++) { //行检测
if (i == column) continue;
if (martix[row][i] == 1)
return true;
}
for (int i = 0; i < count; i++) { //列检测
if (i == row) continue;
if (martix[i][column] == 1)
return true;
}
for (int i = 1; i < count; i++) { //对角线检测，以目标点为中心,对角线相对长度一次检测四个角
if (row - i >= 0 && column - i >= 0) { //左上角
if (martix[row - i][column - i] == 1)
return true;
}
if (row - i >= 0 && column + i < count) { //右上角
if (martix[row - i][column + i] == 1)
return true;
}
if (row + i < count && column - i >= 0) { //左下角
if (martix[row + i][column - i] == 1)
return true;
}
if (row + i < count && column + i < count) { //右下角
if (martix[row + i][column + i] == 1)
return true;
}
}

return result;
}
void backtrackingQueen(int **martix, int n, int count, int *num) {
if (n == count) {
*num += 1;
printf("第%d组解：\n", *num);
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++)
printf("[%d]", martix[i][j]);
printf("\n");
}
printf("\n");
return;
}
for (int i = 0; i < count; i++) { //此循环里2个memset的位置有玄妙
//        memset(martix[n], 0, sizeof(int) * count);
martix[n][i] = 1;
if (!isConfilctQueen(martix, n, i, count))
backtrackingQueen(martix, n + 1, count, num);
memset(martix[n], 0, sizeof(int) * count);
}
}
int nQuenn(int count) {
if (count < 4) {
printf("Error! n < 4\n");
return 0;
}
int **martixQuenn = (int **)malloc(sizeof(int *) * count);
for (int i = 0; i < count; i++) {
martixQuenn[i] = (int *)malloc(sizeof(int) * count);
memset(martixQuenn[i], 0, sizeof(int) * count);
}

int n = 0;
int num = 0;
backtrackingQueen(martixQuenn, n, count, &num);

for (int i = 0; i < count; i++)
free(martixQuenn[i]);
free(martixQuenn);

return num;
}

void printNQueens(int **matrix, int n, int row, int column, int *count) {
if (row >= n || column >= n)
return;

bool flag = true;
for (int i = 0; i < n && flag; i++) {

for (int j = 0; j < n && flag; j++) {
if (matrix[i][j] == 1) {
if (row == i || abs(row - i) == abs(column - j)) {
flag = false;
}
}
}

}

if (flag) {
matrix[row][column] = 1;

if (column == n - 1) { //最后一列到达，开始打印
*count += 1;
printf("第%d组解：\n", *count);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("\n");

matrix[row][column] = 0;

return;
}
}

if (flag) {
printNQueens(matrix, n, 0, column + 1, count);
matrix[row][column] = 0;
}

printNQueens(matrix, n, row + 1, column, count);
}
bool nQueens(int n) {
bool result = true;

int **martrix = (int **)calloc(n, sizeof(int *));
for (int i = 0; i < n; i++) {
martrix[i] = (int *)calloc(n, sizeof(int));
for (int j = 0; j < n; j++)
martrix[i][j] = 0;
}

int *count = (int *)calloc(1, sizeof(int));
int row = 0, column = 0;

printNQueens(martrix, n, row, column, count);
printf("all nQueens are %d.\n\n", *count);

for (int i = 0; i < n; i++) {
free(martrix[i]);
martrix[i] = NULL;
}
free(martrix);
martrix = NULL;

free(count);
count = NULL;

return result;
}

#### 八皇后问题递归解法

2015-08-02 18:25:42

#### 八皇后问题（最简单的递归解法）

2015-08-27 15:24:24

#### 八皇后问题详解(四种解法)

2017-11-08 01:38:49

#### 【数据结构与算法】八皇后问题之递归

2015-08-12 16:31:44

#### 八皇后问题的递归解法（最易理解的版本）

2017-03-23 20:56:52

#### 经典算法<em>八皇后</em>问题的详解以及回溯(<em>递归</em>)代码示例

2018年05月07日 00:00

#### 八皇后问题-递归和迭代两种解法

2012-10-14 14:24:33

#### [免费]<em>八皇后递归解法</em>

2017年09月18日 00:00

#### 八皇后求解方法（递归求解）

2016-04-10 01:11:07

#### C语言回溯法递归求解八皇后问题

2017-10-31 11:32:20