Pagini recente » Cod sursa (job #328528) | Cod sursa (job #1097442) | Cod sursa (job #131038) | Cod sursa (job #2560946) | Cod sursa (job #2601751)
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int N;
int nrsol;
int sol[15];
bool ok (int k)
{
int i;
for (i=1;i<k;++i)
{
if (sol[i]==sol[k] || k-i==abs(sol[k]-sol[i]))
{
return 0;
}
}
return 1;
}
bool eSol (int k)
{
return (k==N);
}
void Afisare (int k)
{
++nrsol;
if (nrsol==1)
{
int i;
for (i=1;i<=k;++i)
{
fout << sol[i] << " ";
}
}
}
void Back (int k)
{
int i;
for (i=1;i<=N;++i)
{
sol[k]=i;
if (ok(k)==1)
{
if (eSol(k)==1)
{
Afisare(k);
}
else
{
Back(k+1);
}
}
}
}
int main()
{
fin >> N;
Back(1);
fout << '\n' << nrsol;
fin.close();
fout.close();
return 0;
}