Cod sursa(job #2828125)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 6 ianuarie 2022 21:45:59
Problema Problema Damelor Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <iostream>

using namespace std;

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

int k, n, col[14];

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

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

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