Cod sursa(job #2833163)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 14 ianuarie 2022 20:55:48
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
#include <iostream>

using namespace std;

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

int k, n, col[14];

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

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

void f(int nivel, int n, int col[])
{
    for (int i = 1; i <= n; i++)
        if (verif(i, nivel, col))
        {
            col[nivel] = i;
            if (nivel == n-1)
            {
                if(!k)
                    afisare(n, col);
                k++;
            }
            else
                f(nivel+1, n, col);
        }
}

int main()
{
    fin >> n;
    f(0, n, col);
    fout << '\n' << k;
    return 0;
}