Cod sursa(job #1743717)

Utilizator Moise_AndreiMoise Andrei Moise_Andrei Data 18 august 2016 16:43:11
Problema Problema Damelor Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("damesah.in");
ofstream out("damesah.out");
int v[15], c[15], a[15], b[15];
int n, nr;

void adaugare(int poz, int n)
{
    if(poz == n)
    {
        if(nr == 0)
        {
            for (int i = 0; i < n; i++)
                out << v[i] + 1 << " ";
            out << '\n';
        }
        ++ nr;
    }
    else
    {
        for(int i = 0; i < n; i++)
        {
            if(c[i] == 0 && a[i - poz + n - 1] == 0 && b[poz + i] == 0)
            {
                v[poz] = i;
                b[poz + i] = 1, c[i] = 1, a[i - poz + n - 1] = 1;
                adaugare(poz + 1, n);
                b[poz + i] = 0, c[i] = 0, a[i - poz + n - 1] = 0;
            }
        }
    }
}
int main()
{
    in >> n;
    adaugare(0, n);
    out << nr;
    return 0;
}