Pagini recente » Cod sursa (job #2402190) | Cod sursa (job #1127192) | Cod sursa (job #504685) | Cod sursa (job #1133426) | Cod sursa (job #2796179)
#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;
}