# 八皇后递归求解

170人阅读 评论(0)

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;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：21487次
• 积分：1466
• 等级：
• 排名：千里之外
• 原创：128篇
• 转载：6篇
• 译文：0篇
• 评论：0条
文章分类
阅读排行
评论排行