Cod sursa(job #1278382)

Utilizator Narcys01Ciovnicu Narcis Narcys01 Data 28 noiembrie 2014 20:04: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], n, nr = 0;

ofstream fout("damesah.out");

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

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

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;
}