Pagini recente » Cod sursa (job #1966958) | Cod sursa (job #966812) | Cod sursa (job #1048613) | Cod sursa (job #1152827) | Cod sursa (job #2776894)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int st[14], n, nr_afis, ap_prin[26], ap_sec[26], ap[14];
void Afisare()
{
nr_afis++;
if (nr_afis <= 1)
{
for (int i = 1; i <= n; i++)
fout << st[i] << " ";
fout << "\n";
}
}
int Valid(int top)
{
if(ap[st[top]] == 1) return 0;
if (st[top] >= top && ap_prin[n - st[top] + top] == 1) return 0;
if (st[top] < top && ap_prin[n + top - st[top]] == 1) return 0;
if (st[top] < n - top + 1 && ap_sec[st[top] + top - 1] == 1) return 0;
if (st[top] >= n - top + 1 && ap_sec[top + st[top] - 1] == 1) return 0;
if (st[top] >= top) ap_prin[n - st[top] + top] = 1;
else ap_prin[n + top - st[top]] = 1;
if (st[top] >= n - top + 1) ap_sec[top + st[top] - 1] = 1;
else ap_sec[st[top] + top - 1] = 1;
ap[st[top]] = 1;
return 1;
}
void Remove(int top)
{
if (st[top] >= top) ap_prin[n - st[top] + top] = 0;
else ap_prin[n + top - st[top]] = 0;
if (st[top] >= n - top + 1) ap_sec[top + st[top] - 1] = 0;
else ap_sec[st[top] + top - 1] = 0;
ap[st[top]] = 0;
}
void Back()
{
int top, cand;
top = 0;
st[++top] = 0;
while (top > 0)
{
cand = 0;
while (cand == 0 && st[top] < n)
{
st[top]++;
cand = Valid(top);
}
if (cand == 0)
{
top--;
Remove(top);
}
else if (top == n)
{
Remove(top);
Afisare();
}
else st[++top] = 0;
}
}
int main()
{
fin >> n;
Back();
fout << nr_afis << "\n";
fin.close();
fout.close();
}