Cod sursa(job #2647825)

Utilizator XLuysSaratean Tudor XLuys Data 6 septembrie 2020 16:26:54
Problema Problema Damelor Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
using namespace std;

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

int N,OK = false;
int J;
int SOL;
int C[20];
void BACKK( int );
void afisare();
void umplere();
int main()
{
    fin >> N;
    BACKK(1);
    fout<<SOL;
    return 0;
}
void BACKK(int k)
{
    if(k == N + 1)
    {
        if(OK == false)
            afisare();
        SOL += 2;
        return;
    }
    int M;
    if(k == 1)
        M = (N+1)/2;
    else M = N;
    for(int i = 1 ; i <= M ; ++i)
    {
        int ok , j;
        for(ok = true , j = 1; j < k && ok == true; ++j)
        {
            if(C[j] == i || (k - j) == abs(C[j] - i))
                ok = false;
        }
        if(ok)
        {
            C[k] = i;
            BACKK(k + 1);
        }
    }
}
void afisare()
{
    for(int i = 1 ; i <= N ; ++i)
        fout<< C[i] << ' ';
    fout<<'\n';
    OK = true;
}