Pagini recente » Cod sursa (job #3245642) | Cod sursa (job #270555) | Cod sursa (job #762422) | Cod sursa (job #2555710) | Cod sursa (job #2141604)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("damesah.in");
ofstream out("damesah.out");
int n, st[17], v[14], sol[14], cnt=0, ok=0;
void Afisare()
{
int i;
cnt++;
if (ok == 0)
{
for (i=1; i<=n; i++)
sol[i] = st[i];
ok = 1;
}
}
void TheRealAfisare()
{
int i;
for (i = 1; i <= n; i++)
out << sol[i] << " ";
out << "\n";
out << cnt << "\n";
}
int Validare(int i, int top)
{
int j;
if (v[i] == 1) return 0;
for (j = 1; j < top; j++)
if (abs(top-j) == abs(i-st[j]))
return 0;
return 1;
}
void Back(int top)
{
int i;
if (top == n+1)
Afisare();
else
for (i=1; i<=n; i++)
if (Validare(i, top))
{
v[i] = 1;
st[top] = i;
Back(top+1);
v[i] = 0;
}
}
int main()
{
in >> n;
Back(1);
TheRealAfisare();
return 0;
}