Cod sursa(job #1815685)

Utilizator MishulikMihai Zgonea Mishulik Data 25 noiembrie 2016 17:36:50
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n, ans;
bool xdame[14];
int ydame[14];
void scrie()
{
    for(int i=1; i<=n; i++)
        fout<<ydame[i]<<' ';
}
bool valid(int k, int i)
{
    bool ret=1;
    for(int j=1; j<k; j++)
        if((ydame[j]==i)||(k-j==abs(i-ydame[j])))
            ret=0;
    return ret;
}
void bcktrk(int k)
{
    if(k!=0)
    {
        for(int i=1; i<=n; i++)
        {
            if(valid(k, i))
            {
                xdame[k]=1;
                ydame[k]=i;
                if(k==n)
                {
                    if(!ans)
                        scrie();
                    ans++;
                }
                else
                    bcktrk(k+1);
            }
        }
    }
}
int main()
{
    fin>>n;
    bcktrk(1);
    fout<<endl<<ans;
}