Cod sursa(job #2028082)

Utilizator crion1999Anitei cristi crion1999 Data 27 septembrie 2017 09:44:33
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fi("damesah.in");
ofstream fo("damesah.out");
int n;
pair<int,int> dame[15];
int dameCount;
int solutions;
bool printed;

void printDame()
{
    for(int i=1;i<=n;++i)
    {
        fo<<dame[i].second<<' ';
    }
    fo<<'\n';
}

bool checkPeace(int i, int j)
{
    if(!dameCount)
        return true;
    for(int k=1; k<=dameCount; ++k)
    {
        if(dame[k].first - i == dame[k].second - j || dame[k].first - i == (-1)*(dame[k].second - j) || dame[k].first == i || dame[k].second == j)
            return false;
    }
    return true;
}
void placeDame(int a)
{
    if(a == n+1 && n == dameCount)
    {
        if(!printed)
        {
            printDame();
            printed = true;
        }
        ++solutions;
    }
    for(int i=1; i<=n; ++i)
    {
        if(checkPeace(a,i))
        {
            ++dameCount;
            dame[dameCount].first = a;
            dame[dameCount].second = i;
            placeDame(a+1);
            --dameCount;
        }
    }
}


int main()
{
    fi>>n;
    placeDame(1);
    fo<<solutions;

}