Cod sursa(job #2418671)

Utilizator dragos.ionita2303Ionita Dragos dragos.ionita2303 Data 5 mai 2019 18:42:12
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int nr, n, v[14], rez[14], l[14], p[28], s[28];

void backtr(int poz) {
    if (poz == n + 1) {
            nr++;
            if (nr == 1) {
                for (int j = 1; j <= n; j++) {
                    rez[j] = v[j];
                }
            }
            return;
    }
    int i;
    for (i = 1; i <= n; i++) {
        if (l[i] == 0 && p[14 + poz - i] == 0 && s[14 + n + 1 - poz - i] == 0) {
            v[poz] = i;
            l[i] = 1;
            p[14 + poz - i] = 1;
            s[14 + n + 1 - poz - i] = 1;
            backtr(poz + 1);
            l[i] = 0;
            p[14 + poz - i] = 0;
            s[14 + n + 1 - poz - i] = 0;
        }
    }
}

int main()
{
    freopen("damesah.in", "r", stdin);
    freopen("damesah.out", "w", stdout);
    int i;
    scanf("%d", &n);
    backtr(1);
    for (i = 1; i <= n; i++) {
        printf("%d ", rez[i]);
    }
    printf("\n%d\n", nr);
    return 0;
}