Cod sursa(job #3366677)

Utilizator theodix_1Tiroiu Theodor Alexandru theodix_1 Data 3 octombrie 2026 12:41:00
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int n, x[14], nrsol = 0, ok = 0;

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

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

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

int main()
{
    f >> n;
    backt();
    g << nrsol << '\n';
    return 0;
}