Cod sursa(job #1551118)

Utilizator CraiuAndrei Craiu Craiu Data 15 decembrie 2015 09:57:24
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("damesah.in");
ofstream fout("damesah.out");

int st[20], n, v[20], cnt;

void Afisare()
{
    cnt++;
    if(cnt == 1)
    {
        for(int i = 1; i <= n; i++)
            fout << st[i] << " ";
        fout <<"\n";
    }
}

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

inline void Back(int top)
{
    int i;
    if(top == n + 1) Afisare();
    else for(i = 1; i <= n; i++)
        if(Valid(top, i))
        {
            v[i] = 1;
            st[top] = i;
            Back(top + 1);
            v[i] = 0;
        }
}

int main()
{
    fin >> n;
    fin.close();
    Back(1);
    fout << cnt <<"\n";
    fout.close();
    return 0;
}