Cod sursa(job #3321551)

Utilizator EricDimiCismaru Eric-Dimitrie EricDimi Data 10 noiembrie 2025 08:52:03
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <cmath>

using namespace std;

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

const int NMAX = 13;

int x[NMAX + 1], n, cnt;

bool valid(int k)
{
    for (int i = 1; i < k; i++)
        if (x[k] == x[i] || k - i == fabs(x[k] - x[i]))
            return 0;
    return 1;
}

void afis()
{
    for (int i = 1; i <= n; i++)
        g << x[i] << ' ';
    g << '\n';
}

void bt()
{
    int k = 1;
    bool ok = 0;
    x[1] = 0;
    while (k > 0)
    {
        if (x[k] < n)
        {
            x[k]++;
            if (valid(k))
            {
                if (k == n)
                {
                    cnt++;
                    if (!ok)
                    {
                        afis();
                        ok = 1;
                    }
                }
                else
                {
                    k++;
                    x[k] = 0;
                }
            }
        }
        else
            k--;
    }
}

int main()
{
    f >> n;
    cnt = 0;
    bt();
    g << cnt;
    f.close();
    g.close();
    return 0;
}