Cod sursa(job #2834016)

Utilizator VladutzPredoiVlad Predoi VladutzPredoi Data 16 ianuarie 2022 11:45:25
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;


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


int cnt;
int n;
int v[20];
bool viz[20];


bool ok(int top,int i)
{
    for(int j=top-1; j>=1; j--)
    {
        if(abs(top-j)==abs(i-v[j]))
        {
            return false;
        }
    }
    return true;
}


void Back(int top)
{
    if(top==n+1)
    {
        cnt++;

        if(cnt==1)
        {
            for(int i=1; i<=n; i++)
            {
                fout<<v[i]<<" ";
            }
            fout<<"\n";
        }

        return;
    }
    for(int i=1; i<=n; i++)
    {
        if(viz[i]==0 && ok(top,i))
        {
            v[top]=i;
            viz[i]=1;
            Back(top+1);
            viz[i]=0;
        }
    }
}


void solve()
{
    fin>>n;
    Back(1);
    fout<<cnt;
}


int main()
{
    solve();
    return 0;
}