[Note]C 八皇后解法

Leaphy 发布于 21 天前 最后更新于 21 天前 160 次阅读


AI 摘要

# 八皇后问题解法 (C语言实现) 以下是一个使用C语言实现的八皇后问题解决方案,采用递归回溯算法来找到所有可能的棋盘布局。 ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <math.h> #define N 8 // 检查是否与已放置的皇后冲突 bool conflict(int state[], int nextColumn) { int nextRow = 0; while (state[nextRow] != -1 && nextRow < N) { nextRow++; } for (int row = 0; row < nextRow; row++) { int column = state[row]; // 检查是否在同一列或对角线上 if (abs(column - nextColumn) == 0 || abs(column - nextColumn) == nextRow - row) { return true; } } return false; } // 美化打印棋盘 void prettyprint(int solution[]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (solution[i] == j) { printf("X "); } else { printf(". "); } } printf("n"); } } // 递归解决八皇后问题 void queens(int row, int state[], int *count) { if (row == N) { (*count)++; printf("第%d种解决方案:n", *count); for (int i = 0; i < N; i++) { printf("%d ", state[i]); } printf("n"); prettyprint(state); printf("**************************************************n"); return; } for (int col = 0; col < N; col++) { if (!conflict(state, col)) { state[row] = col; queens(row + 1, state, count); state[row] = -1; // 回溯 } } } int main() { int state[N]; for (int i = 0; i < N; i++) { state[i] = -1; } int count = 0; queens(0, state, &count); printf("总共找到 %d 种解决方案n", count); return 0; } ``` ## 算法说明 1. **冲突检测**:`conflict()`函数检查在指定列放置皇后是否会与已放置的皇后冲突(同一列或对角线)。 2. **递归回溯**:`queens()`函数使用递归尝试在每一行的不同列放置皇后,当找到有效位置时继续下一行。 3. **解决方案打印**:`prettyprint()`函数以可视化方式显示棋盘布局。 4. **回溯机制**:当无法在当前行找到有效位置时,算法回溯到上一行尝试其他列。 ## 输出示例 程序将输出所有92种可能的八皇后解决方案,每种方案以文本图形方式显示棋盘布局。 这个实现展示了递归和回溯算法在解决约束满足问题中的应用,是理解这些重要算法概念的经典示例。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

#define N 8 

bool conflict(int state[], int nextColumn) {
    int nextRow = 0;
    while (state[nextRow] != -1 && nextRow < N) {
        nextRow++;
    }
    
    for (int row = 0; row < nextRow; row++) {
        int column = state[row];
        // 检查是否在同一列或对角线上
        if (abs(column - nextColumn) == 0 || 
            abs(column - nextColumn) == nextRow - row) {
            return true;
        }
    }
    return false;
}

void prettyprint(int solution[]) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (solution[i] == j) {
                printf("X ");
            } else {
                printf(". ");
            }
        }
        printf("\n");
    }
}

void queens(int row, int state[], int *count) {
    if (row == N) { // 9
        (*count)++;
        printf("第%d种解决方案:\n", *count);
        for (int i = 0; i < N; i++) {
            printf("%d ", state[i]);
        }
        printf("\n");
        prettyprint(state);
        printf("**************************************************\n");
        return;
    }
    
    for (int col = 0; col < N; col++) {
        if (!conflict(state, col)) {
            state[row] = col;
            queens(row + 1, state, count);
            state[row] = -1;  // back
        }
    }
}

int main() {
    int state[N];
    for (int i = 0; i < N; i++) {
        state[i] = -1;
    }
    
    int count = 0;
    queens(0, state, &count);
    printf("总共找到 %d 种解决方案\n", count);
    
    return 0;
}
此作者没有提供个人介绍。
最后更新于 2025-08-25