Cod sursa(job #1470455)

Utilizator aaron72Armand Ioan Anusca Popa aaron72 Data 11 august 2015 12:04:45
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,cnt,sol[15], viz[15];
int st[15];

bool Valid(int top, int i)
{
    if (viz[i] == 1) return false;
    for(int j = 1; j < top; j++)
    {
        if(abs(j-top) == abs(st[j]-i)) return false;
    }
    return true;
}


void Back(int top)
{
    int i;
    if(top==n+1)
    {
        if(cnt==0)
        {
            cnt++;
            for(i=1;i<=n;i++) sol[i] = st[i];
        }
        else
            cnt++;

    }
    else
    for(i=1;i<=n;i++)
        if(Valid(top,i))
        {
            viz[i] = 1;
            st[top] = i;
            Back(top+1);
            viz[i] = 0;
        }
}


int main()
{
    fin>>n;
    Back(1);
    for(int i = 1; i<=n; i++) fout<<sol[i]<<" ";
    fout<<"\n"<<cnt<<"\n";
    fout.close();
    return 0;
}