Pagini recente » Cod sursa (job #2851429) | Cod sursa (job #1978413) | Cod sursa (job #1191053) | Cod sursa (job #1133669) | Cod sursa (job #2796176)
#include <fstream>
#include <stack>
#include <unordered_map>
using namespace std;
ifstream cin("combinari.in");
ofstream cout("combinari.out");
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
// for (std::stack<int> i = st; !i.empty(); i.pop())
// {
// if(i.top()==nr)
// return false;
// }
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()
{
int v[10];
for (struct{stack<int> a;int b;} s = {st, 0}; !s.a.empty(); s.a.pop(),s.b++)
v[s.b] = s.a.top();
///reverse
int left=0,right=k-1;
while(left<right)
{
swap(v[left],v[right]);
left++;
right--;
}
for(int i = 0; i < k; i++)
cout << v[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;
}