Cod sursa(job #1278404)

Utilizator Narcys01Ciovnicu Narcis Narcys01 Data 28 noiembrie 2014 20:36:39
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
#include <algorithm>

using namespace std;

int a[16], j, n, nr = 0;

ofstream fout("damesah.out");

inline bool Valid(int k)
{
    for (j = 1; j < k; j++)
        if (a[j] == a[k] || abs(a[k] - a[j]) == k - j)
            return false;
    return true;
}

void inline Afis()
{
    for (j = 1; j <= n; j++)
        fout << a[j] << " ";
}

void inline Bac(int k)
{
    int i;
    for (i = 1; i <= n; i++)
    {
        a[k] = i;
        if (Valid(k))
        {
            if (k == n)
            {
                nr++;
                if (nr == 1)
                    Afis();
            }
            else Bac(k + 1);
        }
    }
}

int main()
{
    ifstream fin("damesah.in");
    fin >> n;
    Bac(1);
    fin.close();
    fout << "\n" << nr;
    fout.close();
    return 0;
}