Cod sursa(job #1236363)

Utilizator armandpredaPreda Armand armandpreda Data 1 octombrie 2014 21:11:59
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <cstdio>

using namespace std;

int n,sol[15],nr,valid[15],p[30],s[30];

void back(int k);
int main()
{
    freopen("damesah.in","r",stdin);
    freopen("damesah.out","w",stdout);
    scanf("%d",&n);
    back(1);
    printf("\n%d",nr);
    return 0;
}
void back(int k)
{
    int i;
    if(k==n+1)
    {
        nr++;
        if(nr==1)
            for(i=1;i<=n;++i)
                printf("%d ",sol[i]);
    }
    else
        for(i=1;i<=n;++i)
            if(valid[i]==0 and p[k-i+n]==0 and s[k+i]==0)
            {
                valid[i]=1,p[k-i+n]=1,s[k+i]=1;
                sol[k]=i;
                back(k+1);
                valid[i]=0,p[k-i+n]=0,s[k+i]=0;
            }
}