Cod sursa(job #2292305)

Utilizator ALEx6430Alecs Andru ALEx6430 Data 29 noiembrie 2018 12:49:51
Problema Problema Damelor Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>
using namespace std;

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

short n, x[14], l[14], d1[14], d2[14], sol;

void bkt(int k)
{
    if(k-1 == n)
    {
        if(++sol == 1)
        {
            for(int i = 1; i <= n; i++) out << x[i] << ' ';
            out << '\n';
        }
    }
    else
    {
        for(int i = 1; i <= n; i++)
            if(!l[i] && !d1[i-k+n] && !d2[i+k])
            {
                l[i] = d1[i-k+n] = d2[i+k] = 1;
                x[k] = i;

                bkt(k+1);

                l[i] = d1[i-k+n] = d2[i+k] = 0;
            }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    in.tie(0);

    in >> n;

    bkt(1);

    out << sol;

    return 0;
}