Cod sursa(job #3149436)

Utilizator CraiuAndreiCraiu Andrei David CraiuAndrei Data 8 septembrie 2023 16:10:49
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include<fstream>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");

int n,st[20],viz[20],sol;

void Afis()
{
    if(sol==0)
        for(int i=1;i<=n;i++)
            fout<<st[i]<<" ";
    sol++;
}

int Valid(int top, int x)
{

    if(viz[x]==1)return 0;
    for(int i=1;i<top;i++)
        if(top-i==abs(st[i]-x))
            return 0;
    return 1;
}

void Back(int top)
{
    if(top==n+1)Afis();
    else for(int i=1;i<=n;i++)
        if(Valid(top,i))
        {
            viz[i]=1;
            st[top]=i;
            Back(top+1);
            viz[i]=0;
        }
}

int main()
{
    fin>>n;
    Back(1);
    fout<<"\n"<<sol;
    return 0;
}