Cod sursa(job #2429269)

Utilizator ProBatmanBalint Leonard ProBatman Data 8 iunie 2019 18:40:01
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

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

const int CMAX = 20;
int n , v[CMAX] , rasp = 0;
bool ok = 0;

void citire()
{
    fin >> n;
}

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

void bkt(int k)
{
    for(int i=1;i<=n;i++)
    {
        v[k] = i;
        if(verificare(k)==true)
        {
            if(k==n)
            {
                if(ok==0)
                {
                    ok = 1;
                    for(int j=1;j<=n;j++)
                        fout << v[j] << " ";
                }
                rasp++;
            }
            else bkt(k+1);
        }
    }
}

int main()
{
    citire();
    bkt(1);
    fout << '\n' << rasp;
    return 0;
}