Pagini recente » Cod sursa (job #1306509) | Cod sursa (job #3344340) | Cod sursa (job #2666499) | Cod sursa (job #1701162) | Cod sursa (job #3303586)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int solutie[20],n;
long long cnt;
void afisare(int k)
{
for(int i=1;i<=k;++i)
fout<<solutie[i]<<" ";
fout<<'\n';
}
bool valid(int k)
{
bool ok=true;
for(int i=1;i<=k-1;i++)
{
if(solutie[k]==solutie[i])
ok=false;
if(k-i==abs(solutie[k]-solutie[i]))
ok=false;
}
return ok;
}
void bkt(int k)
{
for(int i=1;i<=n;i++)
{
solutie[k]=i;
if(valid(k)==true)
{
if(k==n)
{
cnt++;
if(cnt==1){
afisare(k);
}
}
else
bkt(k+1);
}
}
}
int main()
{
fin>>n;
bkt(1);
fout<<cnt;
return 0;
}