Cod sursa(job #1510216)

Utilizator AcuasPopescu Nicolae-Aurelian Acuas Data 24 octombrie 2015 18:16:00
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <cstdio>
#include <cstdlib>
using namespace std;
int n,i,st[16],j,as,ev,k;
void init(){
    st[k]=0;
}
bool succesor(){
    if(st[k]<n){
        st[k]++;
        return 1;
    }
    return 0;
}
bool valid(){
    for(i=1;i<k;i++)
        if(st[k]==st[i]||abs(st[k]-st[i])==abs(k-i))
            return 0;
    return 1;
}
bool solutie(){
    if(k==n)
        return 1;
    return 0;
}
void tipar(){
    for(i=1;i<=n;i++)
        printf("%d ",st[i]);
    printf("\n");
}
int main()
{
    freopen("damesah.in","r",stdin);
    freopen("damesah.out","w",stdout);
    scanf("%d",&n);
    int contor=0;
    k=1;
    init();
    bool ok=1;
    while(k){
        do{
            as=succesor();
            if(as)
                ev=valid();
        }while(!(as&&ev||!as));
        if(as)
            if(solutie()){
                contor++;
                if(ok!=0){
                    tipar();
                    ok=0;
                }
            }
            else{
                k++;
                init();
            }
        else k--;
    }
    printf("%d ",contor);
    return 0;
}