Pagini recente » Cod sursa (job #2983723) | Cod sursa (job #2751022) | Cod sursa (job #217587) | Cod sursa (job #1077422) | Cod sursa (job #2086878)
#include <iostream>
#include <fstream>
#include <cmath>
#define NMAX 14
using namespace std;
int n;
int st[NMAX], vf;
bool ok() {
for (int i = 1; i < vf; i++)
if ((st[i] == st[vf]) || (abs(st[i] - st[vf]) == vf - i))
return false;
return true;
}
int main()
{
ifstream in("damesah.in");
in >> n;
in.close();
vf = 1;
st[vf] = 0;
long long int nr_sol = 0;
ofstream out("damesah.out");
while (vf != 0) {
st[vf]++;
if (st[vf] > n)
vf--;
else {
if (ok()) {
if (vf == n) {
++nr_sol;
if (nr_sol == 1) {
for (int i = 1; i <= n; i++)
out << st[i] << " ";
out << "\n";
}
} else st[++vf] = 0;
}
}
}
out << nr_sol << "\n";
out.close();
return 0;
}