Advertisement
edutedu

produs cartezian

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