Pagini recente » Cod sursa (job #1268748) | Cod sursa (job #2106435) | Cod sursa (job #2676158) | Cod sursa (job #2550561) | Cod sursa (job #2302449)
#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;
}