Pagini recente » Cod sursa (job #2335131) | Cod sursa (job #2402317) | Cod sursa (job #2899836) | Cod sursa (job #637669) | Cod sursa (job #2796404)
#include <fstream>
#include <stack>
#include <unordered_map>
/// this gets only 80 / 100
/// time limits exceeded
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
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 <= n; i++)
if(fr[i])
cout << i << " ";
cout << '\n';
}
/// 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;
}