Cod sursa(job #2302472)

Utilizator BreakAllPogonaru Stefan BreakAll Data 14 decembrie 2018 18:05:19
Problema Problema Damelor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");

int found = 0, nr = 0;
void back(int poz, vector<int> &P, int n, vector<bool> &Folosit){
    if (poz == n+1){
        for(int i = 0 ; i < n ; i++)
            cout << P[i] << ' ';
        return;
    }
    for(int val = 1 ; val <= n ; val++){
        int OK = 1;
        if(Folosit[val] == 0){
            for(int j = poz - 1 ; j >= 1 ; j--)
                if(abs(poz - j) == abs(val - P[j]))
                    OK = 0;
            if(OK == 1){
                P.push_back(val);
                Folosit[val] = 1;
                back(poz + 1, P, n, Folosit);
                P.pop_back();
                Folosit[val] = 0;
            }
        }
    }
}
int main()
{
    int n;
    fin >> n;
    vector <int> P;
    vector <bool> Folosit(n + 1, false);
    back(1, P, n, Folosit);
    fout << nr;
    return 0;
}