Cod sursa(job #1124985)
Utilizator | Data | 26 februarie 2014 14:57:52 | |
---|---|---|---|
Problema | Problema Damelor | Scor | 80 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.1 kb |
#include <cstdio>
int n,st[100], l = 0;
int abs(int a)
{
if(a>=0)
return a;
else
return -a;
}
int solutie(int k)
{
return k==n;
}
void tipar()
{
for(int i = 1; i<=n; i++)
printf("%d ",st[i]);
}
int valid(int k)
{
int w = 1;
for(int i = 1; i<k; i++)
if(st[i] == st[k] || abs(st[k]-st[i]) == k-i)
w = 0;
return w;
}
void back(int k)
{
for(int i = 1; i<=n; i++)
{
st[k] = i;
if(valid(k))
{
if(l == 0)
{
if (solutie(k))
{
tipar();
l++;
}
else
back(k+1);
}
else
{
if(solutie(k))
l++;
else
back(k+1);
}
}
}
}
int main()
{
freopen("damesah.in","r",stdin);
freopen("damesah.out","w",stdout);
scanf("%d",&n);
back(1);
printf("\n%d",l);
return 0;
}