Cod sursa(job #2833148)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 14 ianuarie 2022 20:26:45
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 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 f(int nivel, int n, int col[])
{
    if (nivel == n)
    {
            if(!k)
            {
                for (int i = 0; i < n; i++)
                    fout << col[i] << ' ';
            }
            k++;
    }
    else
        for (int i = 1; i <= n; i++)
            if(verif(i, nivel, col))
            {
                col[nivel] = i;
                f(nivel+1, n, col);
            }
}

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