Cod sursa(job #1912021)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 7 martie 2017 22:39:54
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <cstdio>
#include <algorithm>
using namespace std;
short n,a[14];
int sol;
short succ(short k)
{
    if(k<=n && a[k]<n)
    {
        a[k]++;
        return 1;
    }
    return 0;
}
short valid(short k)
{
    short i;
    for(i=1;i<=k-1;i++)
        if(a[i]==a[k] || abs(i-k)==abs(a[i]-a[k]))
            return 0;
    return 1;
}
void back()
{
    short i,k;
    k=1;
    while(k)
    {
        i=0;
        while(!i && succ(k))
            if(valid(k))
                i=1;
        if(!i)
            k--;
        else
            if(k==n)
            {
                sol++;
                if(sol==1)
                {
                    for(i=1;i<=n;i++)
                        printf("%hd ",a[i]);
                    printf("\n");
                }
            }
            else
            {
                k++;
                a[k]=0;
            }
    }
}
int main()
{
    freopen("damesah.in","r",stdin);
    freopen("damesah.out","w",stdout);
    scanf("%hd",&n);
    sol=0;
    back();
    printf("%d\n",sol);
    return 0;
}