Cod sursa(job #2796179)

Utilizator db_123Balaban David db_123 Data 7 noiembrie 2021 18:05:22
Problema Combinari Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#include <stack>
#include <unordered_map>

using namespace std;
ifstream cin("combinari.in");
ofstream cout("combinari.out");

const int NMAX = 19;

stack<int> st;
unordered_map<int,bool> fr;

int LIMIT_SOLUTION_SPACE=0;
int n,k;

bool isOK(int nr)
{
    if(st.size()>= LIMIT_SOLUTION_SPACE)
        return false;

    /// number does not exists already in stack
    if(fr[nr])
        return false;

    /// number to be added should be greater then the last one added
    if(st.empty())
        return true;
    else if (nr > st.top())
        return true;
    else
        return false;

    return true;
}
void displayContent()
{
    for(int i = 0; i < NMAX; i++)
    {
        if(fr[i])
            cout << i << " ";
    }

    cout << endl;
}

/// backtracking CHOOSE CONSTRAINT GOAL
/// choose explore unchoose
void back()
{
    /// base condition
    if(st.size()== LIMIT_SOLUTION_SPACE)
    {
        displayContent();
        return;
    }

    /// recursive function

    ///navigate through all solutions
    for(int i = 1 ; i <= n ; i++)
    {
        if(isOK(i))     /// constraint
        {
            st.push(i); /// choose
            fr[i] =  true;
            back();     /// explore
            st.pop();   /// unchoose
            fr[i] =  false;
        }
    }
}
int main()
{
    cin>>n>>k;
    LIMIT_SOLUTION_SPACE = k;

    back();
    return 0;
}