Pagini recente » Cod sursa (job #2073928) | Cod sursa (job #542692) | Cod sursa (job #41534) | Cod sursa (job #1754479) | Cod sursa (job #1239031)
#include <cstdio>
#include <cstring>
#include <cmath>
#define NMAX 15
using namespace std;
int n, P[NMAX], sol[NMAX], noSol;
inline int check(int i)
{
for (int j = 1; j < i; j++)
if (P[j] == P[i] || i - j == abs(P[i] - P[j]))
return 0;
return 1;
}
void back(int k)
{
if (k == n + 1)
{
noSol++;
if (noSol == 1)
memcpy(sol, P, sizeof(P));
return ;
}
for (int i = 1; i <= n; i++)
{
P[k] = i;
if (check(k))
back(k + 1);
}
}
int main()
{
freopen("damesah.in", "r", stdin);
freopen("damesah.out", "w", stdout);
scanf("%d", &n);
back(1);
for (int i = 1; i <= n; i++)
printf("%d ", sol[i]);
printf("\n");
printf("%d\n", noSol);
return 0;
}