Cod sursa(job #2734481)

Utilizator Robys01Robert Sorete Robys01 Data 31 martie 2021 22:57:19
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
#define NMAX 26
using namespace std;

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

short n, a[NMAX];
int nr;
bitset<NMAX> column;
bitset<NMAX * 2> diag1, diag2;

void bk(int k)
{
    if (k == n)
    {
        nr++;
        if (nr == 1){
            for(int i = 0; i<n; i++)
                fout<<a[i] + 1 <<' ';
            fout<<'\n';
        }
        return;
    }
    for (int x = 0; x < n; x++)
    {
        if (column[x] || diag1[x + k] || diag2[x - k + n - 1])
            continue;
        a[k] = x;
        column[x] = diag1[x + k] = diag2[x - k + n - 1] = 1;
        bk(k + 1);
        column[x] = diag1[x + k] = diag2[x - k + n - 1] = 0;
    }
}

int main()
{
    fin >> n;
    bk(0);
    fout << nr;
    return 0;
}