Cod sursa(job #2302449)

Utilizator BreakAllPogonaru Stefan BreakAll Data 14 decembrie 2018 17:42:08
Problema Problema Damelor Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 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){
        int OK = 1;
        for(int i = 0 ; i < n - 1 ; i++)
           for(int j = i + 1 ; j < n ; j++){
                if(abs(i - j) == abs(P[i] - P[j]))
                        OK = 0;
            }
        if(OK == 1 && found == 0){
            for(int i = 0 ; i < n ; i++)
                fout << P[i] << ' ';
            found = 1;
            nr++;
            fout << endl;
        }
        else if(OK == 1 && found == 1)
            nr++;
        return;
    }
    for(int val = 1 ; val <= n ; val++){
        if(Folosit[val] == 0){
            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;
}