Cod sursa(job #2833154)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 14 ianuarie 2022 20:32:09
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 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++)
        {
            bool ok = true;
            for(int j = 0; j < nivel && ok; j++)
                if(i == col[j] || i + nivel == j + col[j] || nivel - i == j - col[j])
                    ok = false;
            if(ok)
            {
                col[nivel] = i;
                f(nivel+1, n, col);
            }
        }
}

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