Pagini recente » Cod sursa (job #121536) | Cod sursa (job #2474029) | Cod sursa (job #555467) | Cod sursa (job #2393549) | Cod sursa (job #1278404)
#include <fstream>
#include <algorithm>
using namespace std;
int a[16], j, n, nr = 0;
ofstream fout("damesah.out");
inline bool Valid(int k)
{
for (j = 1; j < k; j++)
if (a[j] == a[k] || abs(a[k] - a[j]) == k - j)
return false;
return true;
}
void inline Afis()
{
for (j = 1; j <= n; j++)
fout << a[j] << " ";
}
void inline Bac(int k)
{
int i;
for (i = 1; i <= n; i++)
{
a[k] = i;
if (Valid(k))
{
if (k == n)
{
nr++;
if (nr == 1)
Afis();
}
else Bac(k + 1);
}
}
}
int main()
{
ifstream fin("damesah.in");
fin >> n;
Bac(1);
fin.close();
fout << "\n" << nr;
fout.close();
return 0;
}