Advertisement
MaxObznyi

Untitled

Jul 18th, 2022
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int n, k;
  8. bool marked[20];
  9. vector<int> a;
  10.  
  11. ///generate all possible allocations that start from a
  12. void solve() {
  13. if (a.size() == k) {
  14. for (int i = 0; i < a.size(); i++)
  15. cout << a[i] << ' ';
  16. cout << "\n";
  17. return;
  18. }
  19.  
  20. ///try to add one more element
  21. for (int e = 1; e <= n; e++) {
  22. ///try adding to a
  23. if (!marked[e] && (a.size() == 0 || a[a.size() - 1] <= e + 1)) {
  24. a.push_back(e);
  25. ///a[0], a[1], a[2] ... , a[a.size() - 1], e
  26. marked[e] = 1;
  27. solve();
  28. marked[e] = 0;
  29. a.pop_back();
  30. }
  31. }
  32. }
  33.  
  34. int main()
  35. {
  36. cin >> n >> k;
  37. solve();
  38. return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement