Cod sursa(job #1903085)

Utilizator alexandra_paticaAndreea Alexandra Patica alexandra_patica Data 4 martie 2017 23:13:13
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <cstdio>
#include <cstdlib>
using namespace std;

int n, st[20], p, ok, nr, ap[20], ap2[100], ap3[100];

void permut (int k)
{
    for (int x=1; x<=n; x++){
        st[k]=x;
        ap[st[k]]++;
        ap2[k+st[k]]++;
        ap3[st[k]+n-1-k]++;
        if (ap[st[k]]==1 && ap2[k+st[k]]==1 && ap3[st[k]+n-1-k]==1){
//            if (valid(k)){
                if (k==n){
                    nr++;
                    if (!ok){
                        for (int i=1; i<=k; i++)
                            printf("%d ", st[i]);
                        printf("\n");
                        ok=1;
                    }
                }
                else permut(k+1);
//            }
        }
        ap[st[k]]--;
        ap2[k+st[k]]--;
        ap3[st[k]+n-1-k]--;
    }
}

int main (){
    freopen("damesah.in", "r", stdin);
    freopen("damesah.out", "w", stdout);

    scanf("%d", &n);
    permut(1);
    printf("%d", nr);
    return 0;
}