Cod sursa(job #1922842)

Utilizator nicu_serteSerte Nicu nicu_serte Data 10 martie 2017 19:12:45
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n, st[15], nrsol=0, vizcol[15], vdiag1[30], vdiag2[30];
void solutie()
{
    nrsol++;
    if(nrsol==1)
        for(int i=1; i<=n; i++)
            fout<<st[i]<<' ';
}
void bt(int k)
{
    for(int i=1; i<=n; i++)
    {
        st[k]=i;
        if(!vizcol[st[k]] && !vdiag1[st[k]+k-1] && !vdiag2[st[k]-k-1+n])
        {
            vizcol[st[k]]=vdiag1[st[k]+k-1]=vdiag2[st[k]-k-1+n]=1;
            if(k<n)
                bt(k+1);
            else solutie();
            vizcol[st[k]]=vdiag1[st[k]+k-1]=vdiag2[st[k]-k-1+n]=0;
        }
    }
}
int main()
{
    fin>>n; fin.close();
    bt(1);
    fout<<'\n';
    fout<<nrsol<<'\n';
    fout.close();
    return 0;
}