Cod sursa(job #2722105)

Utilizator codrut86Coculescu Ioan-Codrut codrut86 Data 12 martie 2021 16:28:37
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>

using namespace std;

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

const int N = 14;

int n, queen[N], ans;
bool col[N], main_diag[N * 2], sec_diag[N * 2];

void bkt(int l, int n)
{
    if(l == n)
    {
        if(ans < 1)
        {
            for(int c = 0; c < n; c++)
                out << queen[c] + 1 << " ";
            out << "\n";
        }
        ans++;
    }
    else
    {
        for(int c = 0; c < n; c++)
        {
            if(!col[c] && !main_diag[c - l + n - 1] && !sec_diag[l + c])
            {
                queen[l] = c;

                col[c] = main_diag[c - l + n - 1] = sec_diag[l + c] = true;
                bkt(l+1, n);
                col[c] = main_diag[c - l + n - 1] = sec_diag[l + c] = false;
            }
        }
    }
}

int main ()
{
    in >> n;
    bkt(0, n);
    out << ans;
    return 0;
}