Cod sursa(job #3318869)

Utilizator mihaela20Tanasescu Mihaela mihaela20 Data 29 octombrie 2025 14:38:00
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
#include <cmath>
using namespace std;

int n, x[20], nrsol;
int prima[20];
bool ok = 0;

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

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()
{
    nrsol++;
    if(!ok)
    {
        for(int i = 1; i <= n; i++)
            prima[i] = x[i];
        ok = 1;
    }
}

void bt(int k)
{
    if(k <= n)
        for(int i = 1; i <= n; i++)
        {
            x[k] = i;
            if(valid(k))
                bt(k + 1);
        }
    else
        afis();
}

int main()
{
    f >> n;
    bt(1);

    for(int i = 1; i <= n; i++)
        g << prima[i] << ' ';
    g << '\n' << nrsol;

    f.close();
    g.close();
    return 0;
}