#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
long long solve(int row, ull cols, ull ld, ull rd, int n, vector<vector<int>>* solution = nullptr) {
if (row == n) {
if (solution) {
// Store the board configuration if requested
static vector<int> pos(n);
// Note: to fully reconstruct, we'd need to pass positions down,
// but for simplicity, this counts solutions. Modify as needed.
}
return 1;
}
ull mask = (1ULL << n) - 1;
ull avail = ~(cols | ld | rd) & mask;
long long res = 0;
while (avail) {
ull bit = avail & -avail;
avail &= ~bit;
ull newcols = cols | bit;
ull newld = ((ld | bit) << 1) & mask;
ull newrd = ((rd | bit) >> 1) & mask;
res += solve(row + 1, newcols, newld, newrd, n, solution);
}
return res;
}
int main() {
int n;
cout << "Enter n (1 <= n <= 32): ";
cin >> n;
if (n < 1 || n > 32) {
cout << "n must be between 1 and 32." << endl;
return 1;
}
long long num_solutions = solve(0, 0, 0, 0, n);
cout << "Number of solutions for n=" << n << ": " << num_solutions << endl;
// For n=8, expect 92
return 0;
}