Pagini recente » Cod sursa (job #1269103) | Cod sursa (job #1483243) | Cod sursa (job #2261259) | Cod sursa (job #1095366) | Cod sursa (job #2302472)
#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;
}