Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n,k,st[100],m;
  5. void init()
  6. {
  7. st[k]=-1;
  8. }
  9. int succesor()
  10. {
  11. while(st[k]<m-1)
  12. {
  13. st[k]++;
  14. return 1;
  15. }
  16. return 0;
  17. }
  18. int valid()
  19. {if(st[1]==0)
  20. return 0;
  21. return 1;
  22. }
  23. int solutie()
  24. {
  25. return k==n;
  26. }
  27.  
  28. void tipar()
  29. {
  30. for(int i=1;i<=k;i++)
  31. cout<<st[i];
  32. cout<<endl;
  33. }
  34.  
  35. void backtrack()
  36. {
  37. int as;
  38. k=1;
  39. init();
  40. while(k>0)
  41. {as=succesor();
  42. while( as)
  43. {
  44. if(valid()==1)
  45. if(solutie()==1)
  46. tipar();
  47.  
  48. else{
  49. k++;
  50. init();
  51. }
  52. as=succesor();
  53. }
  54.  
  55. k--;
  56. }
  57. }
  58.  
  59.  
  60. int main()
  61.  
  62. {
  63. cin>>n>>m;
  64. backtrack();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement