Pagini recente » Cod sursa (job #1579348) | Cod sursa (job #3266420) | Cod sursa (job #1021130) | Cod sursa (job #1560829) | Cod sursa (job #1778821)
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <iterator>
using namespace std;
vector<int> a, firstSolution;
int countSolutions;
bool isGood(int n) {
for (int i = 0; i < n-1; i++) {
if ( abs(n-1-i) == abs(a[i] - a[n-1]) ) {
return false;
}
}
return true;
}
void generateSolutions(int k) {
if (k >= a.size()) {
if (isGood(a.size())) {
countSolutions++;
if(firstSolution.size() == 0) {
firstSolution = a;
}
}
return;
}
for (int i = k; i < a.size(); i++) {
swap(a[i], a[k]);
if (isGood(k+1)) {
generateSolutions(k+1);
}
swap(a[i], a[k]);
}
}
main() {
ifstream cin("damesah.in");
ofstream cout("damesah.out");
int n;
cin>>n;
for (int i = 0; i < n; i++) {
a.push_back(i+1);
}
generateSolutions(0);
for (int i = 0; i < firstSolution.size(); i++) {
cout<<firstSolution[i]<<" ";
}
cout<<"\n";
cout<<countSolutions;
}