Pagini recente » Cod sursa (job #1396117) | Cod sursa (job #1931636) | Cod sursa (job #2456229) | Cod sursa (job #63467) | Cod sursa (job #1113483)
// Include
#include <fstream>
using namespace std;
// Definitii
#define mp make_pair
#define queen_t pair<int, int>
#define line first
#define column second
// Functii
void back(int currentPos);
bool valide(int currentPos);
bool onSameDiag1(queen_t q1, queen_t q2);
bool onSameDiag2(queen_t q1, queen_t q2);
// Variabile
ifstream in("damesah.in");
ofstream out("damesah.out");
int num;
int stk[15];
int answer;
// Main
int main()
{
in >> num;
back(1);
out << '\n' << answer << '\n';
in.close();
out.close();
return 0;
}
void back(int currentPos)
{
for(stk[currentPos]=1 ; stk[currentPos]<=num ; ++stk[currentPos])
{
if(valide(currentPos))
{
if(currentPos == num)
{
if(++answer==1)
for(int i=1 ; i<=num ; ++i)
out << stk[i] << ' ';
}
else
back(currentPos+1);
}
}
}
bool valide(int currentPos)
{
for(int i=1 ; i<currentPos ; ++i)
{
if(stk[i] == stk[currentPos])
return false;
if(onSameDiag1(mp(i, stk[i]), mp(currentPos, stk[currentPos])))
return false;
if(onSameDiag2(mp(i, stk[i]), mp(currentPos, stk[currentPos])))
return false;
}
return true;
}
bool onSameDiag1(queen_t q1, queen_t q2)
{ return q1.line - q1.column == q2.line - q2.column; }
bool onSameDiag2(queen_t q1, queen_t q2)
{ return q1.line + q1.column == q2.line + q2.column; }