Cod sursa(job #3240733)

Utilizator SilviuC25Silviu Chisalita SilviuC25 Data 20 august 2024 17:29:27
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
using namespace std;

const int MAX_NUM = 15;

int n, answer;
vector<int> arr;
bitset<MAX_NUM * 2> usedCol, usedDiag1, usedDiag2;

ifstream fin("damesah.in");
ofstream fout("damesah.out");

void solve(int row) {
    if (row == n) {
        ++answer;
        if (answer == 1) {
            for (int i = 0; i < n; ++i) {
                fout << arr[i] + 1 << " ";
            }
            fout << "\n";
        }
        return;
    }
    for (int col = 0; col < n; ++col) {
        if (!usedCol[col] && !usedDiag1[row - col + n - 1] && !usedDiag2[row + col]) {
            arr[row] = col;
            usedCol[col] = usedDiag1[row - col + n - 1] = usedDiag2[row + col] = true;
            solve(row + 1);
            usedCol[col] = usedDiag1[row - col + n - 1] = usedDiag2[row + col] = false;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    fin >> n;
    arr.resize(n);
    solve(0);
    fout << answer;
    return 0;
}