Cod sursa(job #2431487)

Utilizator Dragos101Baciu Dragos Dragos101 Data 19 iunie 2019 18:41:49
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>

using namespace std;

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

int n, sol[15],prima, nr;

int Valid(int k)
{
    int i, x, y;
    for(i = 1; i < k; i++)
        if(sol[i] == sol[k]) return 0;
    x=k-1;
    y=sol[k]-1;
    while(x>=1 && y >=1)
        {
            if (sol[x]==y) return 0;
            else {x--;y--;}
        }
    x = k - 1;
    y = sol[k] + 1;
    while(x >= 1 && y <= n)
    {
        if(sol[x] == y) return 0;
        else
        {
            x--;
            y++;
        }
    }
    return 1;
}

void Afisare()
{
    for(int i = 1; i <= n; i++)
        fout << sol[i] << " ";
    fout << "\n";
}

void Back(int k)
{
    int i;
    for(i = 1; i <= n; i++)
    {
        sol[k]=i;
        if(Valid(k)==1)
        {
            if(k == n)
            {
                nr++;
                if(prima==0)
                {
                    Afisare();
                    prima = 1;
                }
            }
            Back(k+1);
        }
    }
}

int main()
{
    fin>>n;
    Back(1);
    fout << nr;
    return 0;
}