Pagini recente » Cod sursa (job #2348108) | Cod sursa (job #354929) | Cod sursa (job #573988) | Cod sursa (job #2290399) | Cod sursa (job #2190123)
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX = 13 + 5;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n, ans;
int queen[NMAX];
bool col[NMAX], main_diag[NMAX * 2], sec_diag[NMAX * 2];
void bkt(int l)
{
if (l == n)
{
if (ans < 1)
{
for (int c = 0; c < n; ++c)
fout << queen[c] + 1 << " ";
fout << '\n';
}
++ans;
}
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;
bkt(l + 1);
col[c] = main_diag[c - l + n - 1] = sec_diag[l + c] = false;
}
}
int main () {
fin >> n;
bkt(0);
fout << ans;
return 0;
}