Pagini recente » Cod sursa (job #355981) | Cod sursa (job #568724) | Cod sursa (job #1352114) | Cod sursa (job #717823) | Cod sursa (job #1248980)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int queen[15], N;
bool col[15], main_diag[30], sec_diag[30];
int answers;
void Backtrack(int l, int n)
{
if (l == n)
{
if (answers < 1)
{
for(int c = 0; c < n; c++)
fout << queen[c] + 1 << " ";
fout << "\n";
}
answers += 1;
}
else
{
for (int c = 0; c < n; c++)
{
if (!col[c] && !main_diag[c - l + n - 1] && !sec_diag[l + c])
{
queen[l] = c;
col[c] = main_diag[c - l + n - 1] = sec_diag[l + c] = true;
Backtrack(l + 1, n);
col[c] = main_diag[c - l + n - 1] = sec_diag[l + c] = false;
}
}
}
}
void Read()
{
fin >> N;
Backtrack(0, N);
fout << answers << "\n";
}
int main()
{
Read();
return 0;
}