Cod sursa(job #2853938)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 20 februarie 2022 19:03:55
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>
#include <iostream>

using namespace std;

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

int k, n, cnt;

bool verif(int nivel, int sol[])
{
    for(int j = 0; j < nivel; j++)
        if(sol[nivel] == sol[j] || sol[nivel] + nivel == j + sol[j] || nivel - sol[nivel] == j - sol[j])
            return false;
    return true;
}

void afisare(int n, int sol[])
{
    for(int i = 0; i < n; i++)
        fout << sol[i] << ' ';
    fout << '\n';
}

void it(int k, int & cnt) {
    int sol[14] = {0};
    int nivel = 0;
    bool afisat = false;
    cnt = 0;

    while (nivel >= 0) {
        if (nivel == k) {
            cnt++;
            if(!afisat){
                afisare(k, sol);
                afisat = true;
            }
            --nivel;
        }
        else{
           ++sol[nivel];
            if (sol[nivel] <= k)
            {
                if(verif(nivel, sol))
                    ++nivel;
            }
            else {
                sol[nivel] = 0;
                --nivel;
            }
        }
    }
}

int main()
{
    fin >> n;
    it(n, cnt);
    fout << cnt;
    return 0;
}