Pagini recente » Cod sursa (job #1712326) | Cod sursa (job #824360) | Istoria paginii runda/o_oji | Atasamentele paginii Clasament christmas_time | Cod sursa (job #1463537)
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
ifstream fin ("damesah.in");
ofstream fout ("damesah.out");
int st[15],v[15];
int n,sol,top;
int Citire()
{
fin>>n;
}
inline int Modul(int x)
{
if (x < 0) return -x;
return x;
}
int Valid(int top, int x)
{
if (v[x] == 1) return 0;
for (int i = 1; i < top; i++)
if (Modul(top-i) == Modul(x-st[i])) return 0;
return 1;
}
void Afisare()
{
int i;
for (i = 1; i <= n; i++)
fout << st[i] << " ";
fout << "\n";
fout << sol << "\n";
fout.close();
exit(0); // cstdlib
}
void Back1(int top)
{
int i;
if (top == n+1) sol++;
else for (i = 1; i <= n; i++)
if (Valid(top, i))
{
st[top] = i;
v[i] = 1;
Back1(top+1);
v[i] = 0;
}
}
void Back(int top)
{
int i;
if (top == n+1) Afisare();
else for (i = 1; i <= n; i++)
if (Valid(top, i))
{
st[top] = i;
v[i] = 1;
Back(top+1);
v[i] = 0;
}
}
int main ()
{
Citire();
Back1(1);
Back(1);
fin.close();
fout.close();
return 0;
}