Cod sursa(job #2251323)

Utilizator bananamandaoneTudor Cosmin Oanea bananamandaone Data 1 octombrie 2018 13:37:59
Problema Dame Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>

using namespace std;

int st[20],n;

void Afisare(int top)
{
    int i,j;
    for(i=1;i<=top;i++)
    {
        for(j=1;j<=top;j++)
            if(st[j] == i) cout<<"* ";
            else cout<<"- ";
        cout<<"\n";
    }
}

int Valid(int top)
{
    int i;
    for(i=1;i<top;i++)
    {
        if(st[i] == st[top]) return 0;
        if(abs(i-top) == abs(st[i]-st[top])) return 0;
    }
    return 1;
}

void Back()
{
    int cand,top;
    top = 1;
    st[top] = 0;
    while(top > 0)
    {
        cand = 0;
        while(cand == 0 && st[top] < n)
        {
            st[top]++;
            cand = Valid(top);
        }
        if(cand == 0) top--;
        else if(top == n)
             {
                 Afisare(top);
                 return ;
             }
             else st[++top] = 0;
    }
}

int main()
{
    cin>>n;
    Back();

    return 0;
}