Cod sursa(job #2774923)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 13 septembrie 2021 16:00:25
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int N, cnt;
int st[15];
bool col[15];
bool valid(int k) {
    for(int i = 1; i < k; i++) {
        if(abs(st[k] - st[i]) == k - i) {
            return 0;
        }
    }
    return 1;
}
void bkt(int k) {
    for(int i = 1; i <= N; i++) {
        if(col[i] == 1) {
            continue;
        }
        st[k] = i;
        col[st[k]] = 1;
        if(valid(k)) {
            if(k == N) {
                cnt++;
                if(cnt == 1) {
                    for(int i = 1; i <= N; i++) {
                        fout << st[i] << " ";
                    }
                    fout << '\n';
                }
            } else {
                bkt(k + 1);
            }
        }
        col[st[k]] = 0;
    }
}
int main() {
    fin >> N;
    bkt(1);
    fout << cnt << '\n';
    return 0;
}