Pagini recente » Cod sursa (job #98578) | Cod sursa (job #442475) | Cod sursa (job #307123) | Cod sursa (job #314417) | Cod sursa (job #2605947)
#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int st[101],cnt,n,ok1;
void afisare()
{
for(int i=1;i<=n;i++)
g<<st[i]<<" ";
g<<"\n";
ok1++;
}
int Validare(int top,int i)
{
int ok=1;
for(int j=1;j<=top-1;j++)
{
if(st[j]==i || abs(top-j)==abs(i-st[j]))
ok=0;
}
if(ok==1)
return 1;
else
return 0;
}
void bkt(int top)
{
if(top==n+1)
{
cnt++;
if(ok1!=1)
afisare();
}
else
{
for(int i=1;i<=n;i++)
if(Validare(top,i))
{
st[top]=i;
bkt(top+1);
}
}
}
int main()
{
f>>n;
bkt(1);
g<<cnt;
}