8皇后92组解

#include <iostream>
using namespace std;
const int N = 8;
int arr[10], total_cnt;
bool isPlaceOK(int *a, int n, int c) {
    for (int i = 1; i <= n - 1; ++i) {
        if (a[i] == c || a[i] - i == c - n || a[i] + i == c + n)
            return false;
    }
    return true;
}
void printSol(int *a) {
    for (int i = 1; i <= N; ++i) { 
        for (int j = 1; j <= N; ++j) { 
            cout << (a[i] == j ? "X" : "-") << " ";;
        } 
        cout << endl;
    }
    cout << endl;
}
 
void addQueen(int *a, int n) {
    if (n > N) { 
        printSol(a);
        total_cnt++;
        return ;
    }
    for (int i = 1; i <= N; ++i) { 
        if (isPlaceOK(a, n, i)) {
            a[n] = i; 
            addQueen(a, n + 1);
        }
    }
}
int main() {
    addQueen(arr, 1);
    cout << "total: " << total_cnt << " solutions.\n";
    return 0;
}