Cod sursa(job #1879997)

Utilizator EuAlexOtaku Hikikomori EuAlex Data 15 februarie 2017 12:28:25
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <cstdio>

using namespace std;

int n, rasp;
int st[20];
bool l[20], d1[40], d2[40];

void bkt(int k) {
    if(k == n + 1) {
        if(rasp == 0) {
            for(int i = 1; i <= n; ++ i) {
                printf("%d ", st[i]);
            }
            printf("\n");
        }
        ++ rasp;
    } else {
        for(int i = 1; i <= n; ++ i) {
            if(!l[i] && !d1[i - k + n] && !d2[i + k - 1]) {
                st[k] = i;
                l[i] = d1[i - k + n] = d2[i + k - 1] = 1;
                bkt(k + 1);
                l[i] = d1[i - k + n] = d2[i + k - 1] = 0;
            }
        }
    }
}

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

    scanf("%d", &n);
    bkt(1);
    printf("%d", rasp);

    return 0;
}