Cod sursa(job #2348504)

Utilizator hoprixVlad Opris hoprix Data 19 februarie 2019 19:36:25
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int x[15], N, sol;
bool gasit;
bool valid(int k) {
    for(int i = 1; i < k; ++i)
        if(x[i] == x[k] || abs(x[i] - x[k]) == k - i)return 0;
    return 1;
}
void bk(int k) {
    for(int i = 1; i <= N; ++i) {
        x[k] = i;
        if(valid(k))
            if(k == N) {
                for(int i = 1; i <= k && !gasit; ++i)
                    fout << x[i] << " ";
                gasit = 1;
                sol++;
            }
            else bk(k + 1);
    }
}
int main() {
    fin >> N;
    bk(1);
    fout << "\n" << sol;
}