Pagini recente » Cod sursa (job #2656196) | Cod sursa (job #932311) | Cod sursa (job #3226755) | Cod sursa (job #1779887) | Cod sursa (job #3165563)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("damesah.in");
ofstream g ("damesah.out");
const int NMAX = 13;
int x[NMAX+1], n, nrsol;
bool col[NMAX+1];
inline bool valid(int k) {
if (col[x[k]])
return 0;
for (int i=1; i < k; i++)
if (k-i == abs(x[k]-x[i]))
return 0;
col[x[k]] = 1;
return 1;
}
inline void afis() {
for (int i=1; i<=n; i++)
g << x[i] << ' ';
g << '\n';
}
void backTracking(int k) {
if (k <= n) {
for (x[k]=1; x[k] <= n; x[k]++)
if (valid(k)) {
backTracking(k+1);
col[x[k]] = 0;
}
} else {
nrsol++;
if (nrsol == 1)
afis();
}
}
int main()
{
f >> n;
backTracking(1);
g << nrsol;
return 0;
}