Cod sursa(job #3366675)

Utilizator theodix_1Tiroiu Theodor Alexandru theodix_1 Data 3 octombrie 2026 12:36:40
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int n, x[14], nrsol, 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 << endl;
}

void backt(int k)
{
    if(k <= n)
        for(int v = 1; v <= n; v++)
        {
            x[k] = v;
            if(valid(k))
                backt(k + 1);
        }
    else
    {
        nrsol++;
        if(ok == 0)
        {
            afis();
            ok = 1;
        }
    }
}
int main()
{
    f >> n;
    backt(1);
    g <<  nrsol;
    return 0;
}