Pagini recente » Cod sursa (job #337316) | Cod sursa (job #1489190) | Cod sursa (job #1221680) | Cod sursa (job #2018290) | Cod sursa (job #1257019)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int st[15],n;
int nr,nr2;
int a[15],lg;
void Afisare(int top)
{
int i,j;
for(i=1;i<=top;i++)
{
for(j=1;j<=top;j++)
if(st[i]==j) //fout<<st[i]<<" ";
{
a[++lg]=st[i];
}
}
}
int Valid(int top)
{
int i;
for(i=1;i<=top-1;i++)
if(st[top]==st[i] || abs(st[top]-st[i])==top-i) return 0;
return 1;
}
void Back()
{
int cand, top;
top=1; st[top]=0;
while(top>0)
{
cand=0;
while(!cand && st[top]<n)
{
st[top]++;
cand=Valid(top);
}
if(!cand )top--;
else
if(top==n) {Afisare(top); nr++;}//exit(0);}
else
{
top++;
st[top]=0;
}
}
}
int main()
{ int i;
fin>>n;
Back();
for(i=1;i<=n;i++)
fout<<a[i]<<" ";
fout<<"\n";
fout<<nr;
return 0;
}