Pagini recente » Cod sursa (job #2247923) | Cod sursa (job #2080567) | Cod sursa (job #2525528) | Cod sursa (job #33558) | Cod sursa (job #1912021)
#include <cstdio>
#include <algorithm>
using namespace std;
short n,a[14];
int sol;
short succ(short k)
{
if(k<=n && a[k]<n)
{
a[k]++;
return 1;
}
return 0;
}
short valid(short k)
{
short i;
for(i=1;i<=k-1;i++)
if(a[i]==a[k] || abs(i-k)==abs(a[i]-a[k]))
return 0;
return 1;
}
void back()
{
short i,k;
k=1;
while(k)
{
i=0;
while(!i && succ(k))
if(valid(k))
i=1;
if(!i)
k--;
else
if(k==n)
{
sol++;
if(sol==1)
{
for(i=1;i<=n;i++)
printf("%hd ",a[i]);
printf("\n");
}
}
else
{
k++;
a[k]=0;
}
}
}
int main()
{
freopen("damesah.in","r",stdin);
freopen("damesah.out","w",stdout);
scanf("%hd",&n);
sol=0;
back();
printf("%d\n",sol);
return 0;
}