Cod sursa(job #2253525)

Utilizator edigabrielTurculet Eduard Gabriel edigabriel Data 4 octombrie 2018 09:22:34
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>

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


int st[25],n;
int viz[25],dp[50],ds[25];
int C;
void Afisare()
{
    C++;
    if(C==1)
    {
        for(int i=1;i<=n;i++)
            fout<<st[i]<<" ";
        fout<<"\n";
    }
}
inline int Valid(int i,int j)
{
    if(viz[j]==1) return 0;
    if(dp[n-j+i]==1) return 0;
    if(ds[i+j-1]==1) return 0;
    return 1;
}
void Backt(int top)
{
    if(top==n+1) Afisare();
    else for(int j=1;j<=n;j++)
             if(Valid(top,j))
                {
                    st[top]=j;
                    viz[j]=1;
                    dp[n-j+top]=1;
                    ds[top+j-1]=1;
                    Backt(top+1);
                    viz[j]=0;
                    dp[n-j+top]=0;
                    ds[top+j-1]=0;
                }
}



int main()
{
    fin>>n;
    Backt(1);
    fout<<C;
    return 0;
}