Pagini recente » Cod sursa (job #1471817) | Cod sursa (job #2761570) | Cod sursa (job #2585436) | Cod sursa (job #640010) | Cod sursa (job #2785394)
#include <fstream>
#include <vector>
#include <set>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n, k;
vector<int> sol;
bool usedCol[50], usedDiag1[50], usedDiag2[50];
int cntSol = 0;
int diagonal1(int x, int y, int n)
{
return x + y + 1;
}
int diagonal2(int x, int y, int n)
{
return x + n - y;
}
void queens(int r)
{
if (r == n)
{
if (cntSol == 0)
{
for (int it : sol)
{
fout << it + 1 << " ";
}
fout << "\n";
}
++cntSol;
return;
}
for (int c = 0; c < n; ++c)
{
if (usedCol[c])
{
continue;
}
int d1 = diagonal1(r, c, n);
if (usedDiag1[d1])
{
continue;
}
int d2 = diagonal2(r, c, n);
if (usedDiag2[d2])
{
continue;
}
usedCol[c] = true;
usedDiag1[d1] = true;
usedDiag2[d2] = true;
if (cntSol == 0)
{
sol.push_back(c);
}
queens(r + 1);
usedCol[c] = false;
usedDiag1[d1] = false;
usedDiag2[d2] = false;
if (cntSol == 0)
{
sol.pop_back();
}
}
}
int main()
{
fin >> n;
queens(0);
fout << cntSol;
return 0;
}