Pagini recente » Cod sursa (job #223487) | Monitorul de evaluare | Istoria paginii utilizator/bubblegumix | Profil M@2Te4i | Cod sursa (job #2681644)
#include <fstream>
#include <cmath>
using namespace std;
ifstream in("damesah.in");
ofstream out("damesah.out");
int n, x[101], nrsol = 0;
bool ln[100], cl[100], dg1[100], dg2[100],f[100];
void afis()
{
for(int i = 1; i <= n; i++)
out << x[i] << ' ';
out << '\n';
}
int valid(int l, int c)
{
if(cl[c] || dg1[l - c + n] || dg2[l + c])return 0;
return 1;
}
void backt(int k)
{
for(int i = 1; i <= n; i++)
{
if(valid(k, i))
{
x[k]=i;
cl[i] = dg1[k - i + n] = dg2[k + i] = 1;
if(k == n)
{
nrsol++;
if(nrsol <= 1)
afis();
}
else
backt(k + 1); //avansare
cl[i] = dg1[k - i + n] = dg2[k + i] = 0;
}
}
}
int main()
{
in >> n;
backt(1);
out << nrsol;
return 0;
}